Help with calculaterish thing


#1

My program is supposed to be a basic calculater, but the addition part isn't working. All advice is very helpful.

var number1 = prompt("Type a number")
var number2 = prompt("Type a number(it can be the same or diffrent.)")

var ask = prompt("Do You want to ADD these numbers, SUBTRACT theese numbers,MULTIPLY theese numbers, or DIVIDE the numbers?(Type the first word)").toUpperCase()
switch(ask){

case'ADD':
var sum = number1 + number2
console.log(sum)
break;

 case'SUBTRACT':
   var diffrence = number1 - number2
   console.log(diffrence)
    break;

    case'MULTIPLY' :
  var product = number1 * number2
  console.log(product)
        break;

        case'DIVIDE':
            var quotient = number1 / number2 
            console.log(quotient)
            break;


            default:
            console.log("PLEASE CHoose an option that was given.")

};


#2

So Hello @microblaster04535! You just missed a basic concept.

So the reason its giving you 24 when you type in 2 and 4 for adding is since they are strings or letters. Basically adding the strings or Concatinating them. So like "Hi" add "Im john" , gives you a result of "Hi Im John".

You need to somehow change them to Integers/Numbers, and javascript provides us a method that changes letters to numbers, whitch is parseInt(string) so if you change your prompts to integers it will work like this:

var number1 = parseInt(prompt("Type a number"))
var number2 = parseInt(prompt("Type a number(it can be the same or diffrent.)"))

Hope this helped!


#3

Thank you very much : )