Inability to showcase text content leads to a div element malfunction

I am seeking assistance in displaying all resulting expressions under the Result and Entered button each time I press "=". I have made some progress but am currently stuck. Any help would be greatly appreciated. Thank you.

<!DOCTYPE html>
<html>
<body>
<head>
</head>
<script>


//Function to perform calculations
function calculation()
{
    var x = parseInt(document.getElementById("op1").value);
    var y = parseInt(document.getElementById("op2").value);
    var z = document.getElementById("operator").value;
    var result;

    if (z == "+"){
        result = x + y;
        document.getElementById("result").value = +result;
    }else if (z == "-"){
        result = x - y;
        document.getElementById("result").value = +result;
    }else if (z == "*"){
        result = x * y;
        document.getElementById("result").value = +result;
    }else if (z == "/"){
        result = x / y;
        document.getElementById("result").value = +result;
    }
    displayResults();
}

function displayResults()
{

    var dispArr = ["document.getElementById('op1').value", "document.getElementById('operator').value", "document.getElementById('op2').value",
    "=","document.getElementById('result').value"];

    dispArr.toString();
    document.getElementbyId("expressions").innerHTML = dispArr.join("");
}

//Function to display the operators
function displayOptr(i) {
    var optrArr =["+","-","*","/"];
    if (i==0){
        document.getElementById("operator").value = "+";
     } else if (i==1){
        document.getElementById("operator").value = "-";
     } else if (i==2){
        document.getElementById("operator").value = "*";
     } else if (i==3){
        document.getElementById("operator").value = "/";    
     }                
}

</script>

<div id="bodyDiv">
        <h1> CALCULATOR </h1>
        <hr/>
        <div class="leftDiv">
            <div id="colorblock">
                <div id = "add" class = "blocks" onClick = "displayOptr(0)">ADD</div>
                <div id = "subtract" class = "blocks" onClick = "displayOptr(1)">SUBTRACT</div>
                <div id = "multiply" class = "blocks" onClick = "displayOptr(2)">MULTIPLY</div>
                <div id = "divide" class = "blocks" onClick = "displayOptr(3)">DIVIDE</div>
            </div>
        </div>
        <div class="rightDiv">
            <div id = "calcblock">
                <input type ="text" size="3" id="op1"/>
                <input type = "text" size="1" id = "operator">
                <input type = "text" size="3"  id="op2"/>
                <input type = "button" value = "="  id="calculate" onClick = "calculation()"/>
                <input type = "text" size="6" id = "result" />
            </div>
        </div>
        <hr/>
        <div class="rightDiv">
            <div id = "pastcalcblock"> 
                <h3> PAST CALCULATIONS </h3> 
                <div id = "resultTab">
                    SORT<br>
                    <input type = "button" value = "As Entered" id = "enteredBut">
                    <input type = "button" value = "By Result" id = "resultBut"><br><br>
                    <div id = "expressions"></hr></div> 

                </div>
            </div>
        </div>
</body>
</html>

Answer №1

The dispArr values need quotes for string treatment, yet the quotes should be removed when using document.getElementbyId instead of document.getElementById on line 39 (beneath dispArr.toString();)

<html>
<body>
<head>
</head>
<script>


//Function to process operations with operators
function calculation()
{
    var x = parseInt(document.getElementById("op1").value);
    var y = parseInt(document.getElementById("op2").value);
    var z = document.getElementById("operator").value;
    var result;

    if (z == "+"){
        result = x + y;
        document.getElementById("result").value = +result;
    }else if (z == "-"){
        result = x - y;
        document.getElementById("result").value = +result;
    }else if (z == "*"){
        result = x * y;
        document.getElementById("result").value = +result;
    }else if (z == "/"){
        result = x / y;
        document.getElementById("result").value = +result;
    }
    displayResults();
}

function displayResults()
{

    var dispArr = [document.getElementById('op1').value, document.getElementById('operator').value, document.getElementById('op2').value,
    "=",document.getElementById('result').value];

    dispArr.toString();
    document.getElementById("expressions").innerHTML = dispArr.join("");
}

//Function to show the operators
function displayOptr(i) {
    var optrArr =["+","-","*","/"];
    if (i==0){
        document.getElementById("operator").value = "+";
     } else if (i==1){
        document.getElementById("operator").value = "-";
     } else if (i==2){
        document.getElementById("operator").value = "*";
     } else if (i==3){
        document.getElementById("operator").value = "/";    
     }                
}

</script>

<div id="bodyDiv">
        <h1> CALCULATOR </h1>
        <hr/>
        <div class="leftDiv">
            <div id="colorblock">
                <div id = "add" class = "blocks" onClick = "displayOptr(0)">ADD</div>
                <div id = "subtract" class = "blocks" onClick = "displayOptr(1)">SUBTRACT</div>
                <div id = "multiply" class = "blocks" onClick = "displayOptr(2)">MULTIPLY</div>
                <div id = "divide" class = "blocks" onClick = "displayOptr(3)">DIVIDE</div>
            </div>
        </div>
        <div class="rightDiv">
            <div id = "calcblock">
                <input type ="text" size="3" id="op1"/>
                <input type = "text" size="1" id = "operator">
                <input type = "text" size="3"  id="op2"/>
                <input type = "button" value = "="  id="calculate" onClick = "calculation()"/>
                <input type = "text" size="6" id = "result" />
            </div>
        </div>
        <hr/>
        <div class="rightDiv">
            <div id = "pastcalcblock"> 
                <h3> PAST CALCULATIONS </h3> 
                <div id = "resultTab">
                    SORT<br>
                    <input type = "button" value = "As Entered" id = "enteredBut">
                    <input type = "button" value = "By Result" id = "resultBut"><br><br>
                    <div id = "expressions"></hr></div> 

                </div>
            </div>
        </div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Come up with various transition animations for multiple child elements that activate when the cursor hovers over the parent element

I have a CSS & HTML code snippet that looks like this: .item-video-kami { width: 480px; overflow: hidden; position: relative; } .item-video-kami>.play-icon.full-height>svg { width: 106px; color: #ffffff50; } .item-video-kami img { ...

It appears that the HTML links are non-responsive and appear to be arranged in an

I have a fairly straightforward website set up. It utilizes php to scan for images and then displays them one by one with navigation buttons at the top. These buttons allow users to switch back and forth between the images. At the top of the page, there ar ...

Prevent popup form from showing again upon page refresh

I currently have a pop-up form that automatically loads when the page is first opened in the browser. Users are required to fill out the form, click submit, and then the form closes. However, if the page is refreshed, the pop-up form reappears. I would lik ...

Mocha struggles to locate the ID within an input field - react-native

I am attempting to retrieve the text of an input using the .text() method in Selenium. However, every time I try to locate the element, it says that the specified ID was not found. (I am working with the Input component from NativeBase, but I have also te ...

Locating the index of the smallest value within an array with the use of the min(a, b

When tasked with finding the index of the minimum value in an array, a common approach is to loop over the array, store the index of the smallest value encountered so far, and update it accordingly: min_index = arr[i] < arr[min_index] ? i : min_index; ...

Guide on how to save a collection of images from a website to a local directory with nodejs

I am looking to retrieve a folder of images and save them to my local directory. I have successfully downloaded a single image by specifying the image name, but I am unsure how to download multiple images at once. Below is the code I have used: var http = ...

Implementing the Fixed Header feature in DataTables - A Step-by-Step Guide

I'm encountering an issue with implementing a fixed header for my table using the DataTables plugin. Despite previously asked questions addressing similar problems, I am still unable to resolve it. I suspect that conflicting CSS or missing CDN links c ...

PHP arrays are returning null upon initialization

I have embarked on a project that involves utilizing HTML5 WebSockets in conjunction with a PHP server equipped with a websockets library found right here on Github. One crucial aspect of my project revolves around monitoring the number of active pla ...

Custom action in Bokeh with a personalized icon

Currently, I am working on creating custom actions for a bokeh plot within the toolbar. In order to achieve this, I require custom icons as well. To begin, I referred to this example which demonstrates creating a drawing tool. So far, everything is functi ...

Creating a Vue.js project with Viddler's top-notch video player for seamless playback and dynamic

Hey there fellow developers! I just dove into learning Vue.js yesterday, and I'm already hooked! I'm currently working on a small application that utilizes Vue (1.0.28) to showcase an online course. You can find a JSFiddle link at the end of thi ...

Adding data to a Vue.JS array from an HTML source

I'm facing a challenge with my assignment where I am struggling to input data from HTML into the VUE.JS array. I have created this form, and now I need assistance in updating the Students array in Vue.js when a user completes the form and clicks on th ...

Navigation bar with horizontal layout

I have the following HTML: <div id="filter-menu"> <div class="filter-item home selected"></div> <div class="filter-item animals"></div> <div class="filter-item ancient"></div> <div class="filte ...

Influence of scoped styles on external webpages

I'm facing an issue with my nuxt app where I have two pages, each with different background styles. However, the background of one page is overriding the other. index.vue <style scoped> body { background-color: #ffffff; } #banner { backgr ...

Ways to display specific HTML content using ng-show according to the current route?

I have a single page using Angular and the layout is as follows: https://i.sstatic.net/XvPQO.png When I click on a tab, for example "Services", the services content is displayed below the tabs. Below is the relevant code snippet: <a href ng-click="t ...

Sending a JavaScript object as a prop in a React component

Currently, I am enrolled in a React course that requires us to pass a single JavaScript object as props to a React application. Here's the code snippet I have been working on: import React from 'react'; import ReactDOM from 'react-dom& ...

Tips for transferring localstorage values from the view to the controller in order to utilize them as a PHP variable in a separate view

How can I pass the value from local storage as a PHP variable when using location.href in the controller after clicking a button? In the view: echo Html::button('Proceed', [ 'onclick' => " var dataVal = localStorage.g ...

Vue.js Contact Form Issue: Error message - 'Trying to access 'post' property of an undefined object'

Currently, I am encountering the error 'cannot read property 'post' of undefined' in my code, but pinpointing the exact mistake is proving to be a challenge. Given that I am relatively new to Vue JS, I would greatly appreciate it if som ...

Assistance required in designing a unique shape using Three.js

https://i.sstatic.net/pmHP2.png Hello there! I could use some assistance with replicating the 2D shape shown in the image above using three.js. Specifically, I'm encountering some difficulty with incorporating the subtle curvature seen on the inner l ...

Angular Universal causing issues with updating the DOM component

@Component({ selector: 'mh-feature-popup', template: ` <div class="full"> <div> <div class="container-fluid" [@featurepop]="state"> <div class="row"> <div class="col-xs-12 col-md-4 col-md-offse ...

Problem with TypeScript involving parameter destructuring and null coalescing

When using parameter destructuring with null coalescing in TypeScript, there seems to be an issue with the optional name attribute. I prefer not to modify the original code like this: const name = data?.resource?.name ?? [] just to appease TypeScript. How ...