+ 11
My javascript calculator doesn't work.
It executes the first if statement regardless of the first input. Please help me. https://code.sololearn.com/W15ANJ6f9Bgn/#js
20 Respuestas
+ 3
@Whitman OK, figured it out (I think). Typing in something random first,skips the addition if statement and continues to the subtraction if statement which because of the errors in it (see my first comment) runs and subtracts instead. Please reply back so I know wether or not this is solved.
+ 10
Try it now everyone. It works now. Thanks to everyone for helping. I know this isn't the easiest way to make a calculator, but I'm just using this for practice.
+ 8
@James16
It always adds the numbers. 2+2=4. Try different numbers.
+ 7
@Spencer
Ok, but why does the code run the first if statement regardless of which operation you input? If you enter something random first, it still runs the addition section of the code.
+ 6
/*Try the function "eval" in your calculator!
Writes 5 + 5 without using the "="
Syntax:*/
var x = prompt ("Your number"); alert (eval (x));
/*@Sololearn*/
+ 6
@Mohamed Henda
That is not a syntax error. It's meant to be like that. It does the first number you input to the power of the second number inputted. There are no longer problems with the code. They have all been fixed.
+ 6
I fixed the code so there is no longer a supposed syntax error in line 29.
+ 5
@Mohamed Henda
I noticed that. I'm not sure why it says that. It also says I'm missing a semicolon, even though I'm not. The code works exactly how it's supposed to. The code does not work correctly when I change "**" to "*".
+ 5
@Mohamed Henda
As I have have previously said, there are no problems with the code. The problems the code had when I made this post have already been fixed.
+ 4
What are you speaking about? At you all perfectly works! for example, I entered 2 * 2, and I was given 4, excellent work! :)
@Sololearn
+ 4
@James16 True, you could do that but in real applications it is not safe to use.
+ 3
You can not do if (myWord1 === "division" || "Division" || "divide" || "Divide")
You have to do if (myWord1 === "division" || myWord1 === "Division" || myWord1 === "divide" || myWord1 === "Divide")
The first if statement runs every time because a string with text in it returns true, satisfying the condition. You probably just got confused on how the or operator works.
+ 2
@Whitman I did not notice that at first. Not sure why it does that. Will try to continue looking at the program.
+ 1
@Whitman No problem. I am always willing to help out my fellow programmers.
+ 1
in the line nember 29 ther's a syntax error you write
var result = myNum1 ** myNum2;
try to write
var result = myNum1 * myNum2;
+ 1
but when i run the code in my devies ther show an error message in line 29
+ 1
when i chenge "**" to "*" the code work correctly
+ 1
so what is the problem, the calculator is work correctly
+ 1
so what is the problem, the calculator is work correctly
+ 1
ok good
I want to tell you try to use switch instead of if else