I'm new to js/programming and just making a simple calculator using prompt. I'm trying to validate that what's been entered are numbers and not strings. I tried this
var operatorType = prompt("Do you want to add, subtract, multiply or divide?").toLowerCase();
switch (operatorType) {
case 'add':
var i = prompt("Enter your first number");
var j = prompt("Enter your second number");
if (isNaN(i) === false) && (isNaN(j) === false) {
document.write(i+" plus "+j+" equals "+(i+j));
} else {
document.write("You didn't enter two numbers.");
}
break;
As well as if (i != 'string') && (j != 'string')
but I keep getting "Unexpected token &&". I looked it up and if/else within a case is valid so I'm not sure what I'm doing wrong.
Full code if it helps
var operatorType = prompt("Do you want to add, subtract, multiply or divide?").toLowerCase();
switch (operatorType) {
case 'add':
var i = prompt("Enter your first number");
var j = prompt("Enter your second number");
if (isNaN(i) === false) && (isNaN(j) === false) {
document.write(i+" plus "+j+" equals "+(i+j));
} else {
document.write("You didn't enter two numbers.");
}
break;
case 'subtract':
var i = prompt("Enter your first number");
var j = prompt("Enter your second number");
document.write(i+" minus "+j+" equals "+(i-j));
break;
case 'multiply':
var i = prompt("Enter your first number");
var j = prompt("Enter your second number");
document.write(i+" multiplied by "+j+" equals "+(i*j));
break;
case 'divide':
var i = prompt("Enter your first number");
var j = prompt("Enter your second number");
document.write(i+" divided by "+j+" equals "+(i/j));
break;
default:
document.write("Please enter whether you want to add, subtract, multiply or divide.");
break;
}
I have an AngulaarJS application where users select the day they want to view and it prints on the calendar the day and the next 30 days. The intended functionality needs to be so that when a user ...
I have an AngulaarJS application where users select the day they want to view and it prints on the calendar the day and the next 30 days. The intended functionality needs to be so that when a user ...
I would like to get a list of all the properties, styles, events and methods of an HTML element. Is there a reflection API in the browsers? I'm looking for something like this: var button = new ...
I would like to get a list of all the properties, styles, events and methods of an HTML element. Is there a reflection API in the browsers? I'm looking for something like this: var button = new ...
My function is like below. Consider for high performance, how can I avoid to add text in a loop structure in html? Please advise. Thank you so much. function createDiv (array) { var i; var ...
My function is like below. Consider for high performance, how can I avoid to add text in a loop structure in html? Please advise. Thank you so much. function createDiv (array) { var i; var ...
I have an assignment to create a battleship game, with 3x3 - 10x10 grid. My current idea would be to create a grid with a click of a button, or atlest change the current size with a click of a button. ...
I have an assignment to create a battleship game, with 3x3 - 10x10 grid. My current idea would be to create a grid with a click of a button, or atlest change the current size with a click of a button. ...