Need help In calculator app

i cant get input text ( Now resolved )

Anybody who is online please help

Hello there,

You need to think about when you want what to run. All of this only runs once (when the script is loaded)…never again:

var numone = document.getElementById("input1").value;
var numtwo = document.getElementById("input2").value;
var num1parsed = parseInt(numone);
var num2parsed = parseInt(numtwo);
var answer = 0;
var answer1 = (document.getElementById("answer").innerHTML = answer);

Therefore, answer is always 0, numone and numtwo are always empty strings ('').

Also, what do you expect this line to do?:

var answer1 = (document.getElementById("answer").innerHTML = answer);

i was thinking this was the best idea to do it.any way to fix it cus i want to make a calculator app with only 4 buttons namely DMAS. can you tell me what to do.I expected this line to give answer and it gave but it gave 0

I will not give you answers. So, I have already told you what is the main issue:

  • You have all the pieces you need (to get started)
  • Now, realise:
  • Work with this. Change it so the code runs more than once…think about when you want it to run.

You are close. Perhaps, step away from the code, and just think: “What workflow do I want?” - how do you expect the app to be used, and what needs to happen in the code for that to work.

Hope this helps

I’ll add another hint - you only want the calculator to calculate something after someone presses a button, so you’ll need an event listener:

Sorry for replying late,I live in IST but in the night I figured out how to do this . I thought that I will create a different JavaScript file for each function and link it to the specific button. Will that be good. Also I asked for help because I am a beginner knowing only HTML and CSS. I am currently working on learning javascript python and C# so I thought I would try working on this my idea Good?

A post was split to a new topic: JavaScript hasOwnProperty Usage

It’s not really necessary to have separate files for separate functions, in fact it would make more sense to keep them in one file if your functions all deal with the same application (calculator).

Uh…yes they do but I was wondering that whenever I make script.js it always loads first so I am thinking that I could make it. Is it workable cus I want to built the calculator under my own knowledge. Pls tell if it will work @jsdisco

I’m not sure I understand the question, I think there’s some basic misunderstanding about how to handle interactivity with JavaScript.

For example, you’d do something like this:

// grab a reference to your equals-button
const equalsButton = document.querySelector('#equals-button');

// add an event listener, so that whenever someone clicks the button,
// the function "evaluate" will be called and calculate the result
equalsButton.addEventListener('click', evaluate)

function evaluate(number1, number2){
    // calculate result

So, yes, the script will always load first, but the functions to add/substract/evaluate etc. will only be called when someone clicks a button.

I think I have solved my issue myself because instead of putting the code which was outside th function, i put it inside the function and it worked and i created 4 function like that website link is
I want to ask your github username jsdisco so i can add you as a contributer in my github repository

Repo Link Is :-

I have online classes as well so I replied late cus I am only in 7th class

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.