+ 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

22nd Dec 2017, 4:32 AM
PlanetGK
PlanetGK - avatar
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.
22nd Dec 2017, 4:56 AM
Spencer
+ 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.
22nd Dec 2017, 5:05 AM
PlanetGK
PlanetGK - avatar
+ 8
@James16 It always adds the numbers. 2+2=4. Try different numbers.
22nd Dec 2017, 4:43 AM
PlanetGK
PlanetGK - avatar
+ 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.
22nd Dec 2017, 4:49 AM
PlanetGK
PlanetGK - avatar
+ 6
/*Try the function "eval" in your calculator! Writes 5 + 5 without using the "=" Syntax:*/ var x = prompt ("Your number"); alert (eval (x)); /*@Sololearn*/
22nd Dec 2017, 4:50 AM
James16
James16 - avatar
+ 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.
24th Dec 2017, 12:20 AM
PlanetGK
PlanetGK - avatar
+ 6
I fixed the code so there is no longer a supposed syntax error in line 29.
24th Dec 2017, 12:45 AM
PlanetGK
PlanetGK - avatar
+ 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 "*".
24th Dec 2017, 12:27 AM
PlanetGK
PlanetGK - avatar
+ 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.
24th Dec 2017, 12:35 AM
PlanetGK
PlanetGK - avatar
+ 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
22nd Dec 2017, 4:41 AM
James16
James16 - avatar
+ 4
@James16 True, you could do that but in real applications it is not safe to use.
22nd Dec 2017, 4:52 AM
Spencer
+ 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.
22nd Dec 2017, 4:40 AM
Spencer
+ 2
@Whitman I did not notice that at first. Not sure why it does that. Will try to continue looking at the program.
22nd Dec 2017, 4:54 AM
Spencer
+ 1
@Whitman No problem. I am always willing to help out my fellow programmers.
22nd Dec 2017, 5:06 AM
Spencer
+ 1
in the line nember 29 ther's a syntax error you write var result = myNum1 ** myNum2; try to write var result = myNum1 * myNum2;
24th Dec 2017, 12:13 AM
Mohamed Handa
Mohamed Handa - avatar
+ 1
but when i run the code in my devies ther show an error message in line 29
24th Dec 2017, 12:24 AM
Mohamed Handa
Mohamed Handa - avatar
+ 1
when i chenge "**" to "*" the code work correctly
24th Dec 2017, 12:25 AM
Mohamed Handa
Mohamed Handa - avatar
+ 1
so what is the problem, the calculator is work correctly
24th Dec 2017, 12:31 AM
Mohamed Handa
Mohamed Handa - avatar
+ 1
so what is the problem, the calculator is work correctly
24th Dec 2017, 12:32 AM
Mohamed Handa
Mohamed Handa - avatar
+ 1
ok good I want to tell you try to use switch instead of if else
24th Dec 2017, 12:47 AM
Mohamed Handa
Mohamed Handa - avatar