hi everyone i am jonnathan i am trying to create a calculator in javascript and i am following a tutorial on youtube but for some reason if i add 1 and 1 together the result 2 is alerting
var number1 = prompt("Input the first number");
var op = prompt("Input operator")
var number2 = prompt("Input 3rd number")
if (op == "+") {
if (number1 == "1") {
if (number2 == "1") {
alert("2");
}
if (number2 == "2") {
alert("3");
}
if (number2 == "3") {
alert("4");
}
if (number2 == "4") {
alert("5");
}
if (number2 == "5") {
alert("6");
}
if (number2 == "6") {
alert("7");
}
if (number2 == "7") {
alert("8");
}
if (number2 == "8") {
alert("9");
}
if (number2 == "9") {
alert("10");
}
}
if (number2 == "1") {
if (number1 == "1") {
alert("2");
}
if (number1 == "2") {
alert("3");
}
if (number1 == "3") {
alert("4");
}
if (number1 == "4") {
alert("5");
}
if (number1 == "5") {
alert("6");
}
if (number1 == "6") {
alert("7");
}
if (number1 == "7") {
alert("8");
}
if (number1 == "8") {
alert("9");
}
if (number1 == "9") {
alert("10");
}
}
}
twice there are no errors in the console either that i think could help me with this issue
so far the calculator only adds numbers and only does it through 1 ti 9. how can i fix this error?
In your code:
if (op == "+") {
if (number1 == "1")
{
if (number2 == "1")
{
alert("2");
}
This section is alerting "2" first. And then this section:
if (number2 == "1") {
if (number1 == "1") {
alert("2");
}
That's why it's appearing twice.
You can write the program as:
let number1 = parseInt(prompt("Input the first number"));
let op = prompt("Input the operator");
let number2 = parseInt(prompt("Input the second number"));
if (op == '+')
alert(number1 + number2);
else if (op == '-')
alert(number1 - number2);
else if (op == '*')
alert(number1 * number2);
else if (op == '/')
alert(number1 / number2);
else
alert("Please enter a valid operator");
Here we used parseInt(), that returns the first integer from a string.
Everything works well but the equals sign do not work. Does anyone know why and how to fix it? I am wondering why the eval('=') won't work in this case.
function calcu(calcValue) {
if (calcValue == '1') {
calc.output.value = '1';
} else if (calcValue == '2') {
calc.output.value = '2';
} else if (calcValue == '3') {
calc.output.value = '3';
} else if (calcValue == '+') {
calc.output.value = '+';
} else if (calcValue == '4') {
calc.output.value = '4';
} else if (calcValue == '5') {
calc.output.value = '5';
} else if (calcValue == '6') {
calc.output.value = '6';
} else if (calcValue == '-') {
calc.output.value = '-';
} else if (calcValue == '7') {
calc.output.value = '7';
} else if (calcValue == '8') {
calc.output.value = '8';
} else if (calcValue == '9') {
calc.output.value = '9';
} else if (calcValue == '*') {
calc.output.value = '*';
} else if (calcValue == '') {
calc.output.value = '';
} else if (calcValue == '0') {
calc.output.value = '0';
} else if (calcValue == '/') {
calc.output.value = '/';
} else // <-- Notice no condition on this last else
{
calc.output.value = eval('='); // <-- the eval() function turns the collection of string into a working math function
}
}
I'm trying to create a calculator program for my assignment course in JavaScript. The program has to calculate to numbers (values assigned by the user), as well as the operator (+, - , * ,/), and to display the result.
If the user input an extra character in the field like (15n + 20 or 15 ++ 20 or 15+ + 20) has to display an error "Cannot concatenate a number with a string"
if the user divide a number by 0 has to display "Division by 0 is not allowed"
Please I really need your help.
I posted, my code bellow, let me know. What I'm doing wrong?
I want to mention that I'm at the beginning level.
Thanks, I appreciate all the advices
let numberOne = parseFloat(prompt("First Number: "));
let operator = prompt("Chose operation (+ - * /): ");
let numberTwo = parseFloat(prompt("Second Number: " ));
function DivisionByZero(a, b) {
if (a == 0 || b == 0 == true) {
throw "Divizion by 0 is not allowed";
}
}
function TypingError(a, b, c) {
if (numberOne + " " == true) {
throw "Cannot concatenate a number with a string";
} else if (numberTwo + " " == true) {
throw "Cannot concatenate a number with a string";
} else if (operator + operator == true) {
throw "Can not concatenate two operators"
}
}
let TypingErrorDisplay = TypingError(numberOne, operator, numberTwo);
var ErrorDivisionDisplay = DivisionByZero(numberOne, numberTwo);
if (operator === "+") {
var calc = parseFloat(numberOne) + parseFloat(numberTwo);
alert(calc);
}
else if (operator === "-") {
var calc = parseFloat(numberOne) - parseFloat(numberTwo);
alert(calc);
}
else if (operator === "*") {
var calc = parseFloat(numberOne) * parseFloat(numberTwo);
alert(calc);
}
else if (operator === "/") {
var calc = parseFloat(numberOne) / parseFloat(numberTwo);
alert(calc);
if (ErrorDivisionDisplay == true) {
alert("Division by 0 is not allowed");
}
}
else {
alert(TypingErrorDisplay);
}
First of all,
in your first condition you have
function DivisionByZero(a, b) {
if (a == 0 || b == 0 == true) {
throw "Divizion by 0 is not allowed";
}
}
You can't have b == 0 == true.
You just need :
function DivisionByZero(a, b) {
if (a === 0 || b === 0) {
throw "Divizion by 0 is not allowed";
}
}
for the rest, you should check that post : Check if a variable is a string in JavaScript
function TypingError(a, b, c) {
if (typeOf(numberOne) == "string") {
throw "Cannot concatenate a number with a string";
}
else if (typeOf(numberTwo) === "string") {
throw "Cannot concatenate a number with a string";
}
else if (operator.lenght > 1) {
throw "Can not concatenate two operators"
}
}
Try to correct that things fisrt and tell us.
Axel,
let numberOne = parseFloat(prompt("First Number: "));
let operator = prompt("Chose operation (+ - * /): ");
let numberTwo = parseFloat(prompt("Second Number: " ));
function DivisionByZero(a, b) {
if (a == 0 || b == 0) { // You don't need == true, you already declared the conditions
throw "Divizion by 0 is not allowed";
}
}
function TypingError(n1, op, n2) {
if (!n1.isFinite()) { // Triggers if numberOne is not finite
throw "Cannot concatenate a number with a string";
} else if (!n2.isFinite()) { // Triggers if numberTwo is not finite
throw "Cannot concatenate a number with a string";
} else if (op != "+" && op != "-" && op != "*" && op != "/") { // Accept only valid arithmetic operators
throw "Can not concatenate two operators"
}
}
let TypingErrorDisplay = TypingError(numberOne, operator, numberTwo);
var ErrorDivisionDisplay = DivisionByZero(numberOne, numberTwo);
if (operator === "+") {
var calc = parseFloat(numberOne) + parseFloat(numberTwo);
alert(calc);
}
else if (operator === "-") {
var calc = parseFloat(numberOne) - parseFloat(numberTwo);
alert(calc);
}
else if (operator === "*") {
var calc = parseFloat(numberOne) * parseFloat(numberTwo);
alert(calc);
}
else if (operator === "/") {
var calc = parseFloat(numberOne) / parseFloat(numberTwo);
alert(calc);
if (ErrorDivisionDisplay == true) {
alert("Division by 0 is not allowed");
}
}
else {
alert(TypingErrorDisplay);
}
#Solution#
The solution that I found it is displayed below. Please let me know what you think about it.
let numberOne = prompt("First Number: ");
let operator = prompt("Chose operation (+ - * /): ");
let numberTwo = prompt("Second Number: ");
function TypingError(op1, op2, operation) {
if (isNaN(numberOne) || numberOne.length == 0 || isNaN(numberTwo) || numberTwo.length == 0) {
return -1;
}
if (operation !== undefined && operation.length > 1) {
return -2;
}
return 0;
}
let TypingErrorDisplay = TypingError(numberOne, numberTwo, operator);
if (TypingErrorDisplay == -1) {
alert("one of the operands is not numeric ");
}
else if (TypingErrorDisplay == -2) {
alert("wrong operation - symbol must have one character");
}
else {
numberOne = parseFloat(numberOne);
numberTwo = parseFloat(numberTwo);
if (operator == "+") {
var calc = numberOne + numberTwo;
alert(calc);
}
else if (operator == "-") {
var calc = numberOne - numberTwo;
alert(calc);
}
else if (operator == "*") {
var calc = numberOne * numberTwo;
alert(calc);
}
else if (operator === "/") {
if (numberTwo == 0) {
alert("Division by 0 is not allowed");
}
else {
var calc = numberOne / numberTwo;;
alert(calc);
}
}
else {
alert("operation " + operator + " not exists");
}
}
I'm making a puzzle game where the user uses WASD to move a character up, left, down, right respectively. It works fine at the moment but I was wondering if there was a way to break the code down into more intuitive functions. Below is my code:
function move(e)
{
for (var y = 0; y < mapHeight; y++) {
for (var x = 0; x < mapWidth; x++) {
if (map[y][x] == "#" || map[y][x] == "+") {
break;
}
}
if (map[y][x] == "#" || map[y][x] == "+") {
break;
}
}
var player_x = x;
var player_y = y;
if (e.key == 'w') {
var player_new_x = player_x;
var player_new_y = player_y - 1;
if (moveBox(player_new_x, player_new_y, "up") === false) {
return;
}
if (map[player_new_y][player_new_x] == " " ||
map[player_new_y][player_new_x] == ".") {
if (map[player_new_y][player_new_x] == " ") {
map[player_new_y][player_new_x] = "#";
} else if (map[player_new_y][player_new_x] == ".") {
map[player_new_y][player_new_x] = "+";
}
if (map[player_y][player_x] == "#") {
map[player_y][player_x] = " ";
} else if (map[player_y][player_x] == "+") {
map[player_y][player_x] = ".";
}
}
} else if (e.key == 's') {
var player_new_x = player_x;
var player_new_y = player_y + 1;
if (moveBox(player_new_x, player_new_y, "down") === false) {
return;
}
if (map[player_new_y][player_new_x] == " " ||
map[player_new_y][player_new_x] == ".") {
if (map[player_new_y][player_new_x] == " ") {
map[player_new_y][player_new_x] = "#";
} else if (map[player_new_y][player_new_x] == ".") {
map[player_new_y][player_new_x] = "+";
}
if (map[player_y][player_x] == "#") {
map[player_y][player_x] = " ";
} else if (map[player_y][player_x] == "+") {
map[player_y][player_x] = ".";
}
}
} else if (e.key == 'a') {
var player_new_x = player_x - 1;
var player_new_y = player_y;
if (moveBox(player_new_x, player_new_y, "left") === false) {
return;
}
if (map[player_new_y][player_new_x] == " " ||
map[player_new_y][player_new_x] == ".") {
if (map[player_new_y][player_new_x] == " ") {
map[player_new_y][player_new_x] = "#";
} else if (map[player_new_y][player_new_x] == ".") {
map[player_new_y][player_new_x] = "+";
}
if (map[player_y][player_x] == "#") {
map[player_y][player_x] = " ";
} else if (map[player_y][player_x] == "+") {
map[player_y][player_x] = ".";
}
}
} else if (e.key == 'd') {
var player_new_x = player_x + 1;
var player_new_y = player_y;
if (moveBox(player_new_x, player_new_y, "right") === false) {
return;
}
if (map[player_new_y][player_new_x] == " " ||
map[player_new_y][player_new_x] == ".") {
if (map[player_new_y][player_new_x] == " ") {
map[player_new_y][player_new_x] = "#";
} else if (map[player_new_y][player_new_x] == ".") {
map[player_new_y][player_new_x] = "+";
}
if (map[player_y][player_x] == "#") {
map[player_y][player_x] = " ";
} else if (map[player_y][player_x] == "+") {
map[player_y][player_x] = ".";
}
}
} else {
return;
}
render();
}
Is it possible to make four functions, one for each of the movement keys? Any help would be much appreciated
Yes
It´s possible to do what you want, 4 different functions
But ... you should intercept the events keydown (when the user presses the key) and keyup (when the user releases the key)
As long as the key is "pressed" you do the movement
You can create an object like this
let move = { moveH : 0 , moveV :0 }
When the keydown event is detected for "a" -> {moveH : -1, moveV :0}
"s" -> { moveH :0 , moveV :1 }
"w" -> { moveH :0 , moveV :-1 }
"d" -> { moveH :1 , moveV :0 }
When the keyup event is detected .. for any key -> {moveH :0 , moveV:0 }
Meanwhile
apply the move to the object on the screen
something like
stuff.position = { x : stuff.position.x + move.moveH , y: stuff.position.y + move.moveV }
I'm trying to get the quiz to loop 5 times while recording the correct answer for each question before returning to start menu but I'm struggling to get it working
Any help with this will be much appreciated.
function cleartxt()
{
setTimeout("document.getElementById('ans').innerHTML = ''", 3000);
}
var random = new Array(5);
var count = 0;
function next()
{
var store = 0;
do
{
store = (Math.round(Math.ceil(Math.random() * 40) -1));
}while(random.indexOf(store) > -1);
document.getElementById("ques").innerHTML = questions[store][0];
document.getElementById("rad1").innerHTML = questions[store][1];
document.getElementById("rad2").innerHTML = questions[store][2];
document.getElementById("rad3").innerHTML = questions[store][3];
document.getElementById("rad4").innerHTML = questions[store][4];
document.getElementById("image").src = images[store];
var radio = document.getElementsByName("rad");
while(store <= 5)
{
count++;
if(store == 5)
startMenu();
if(radio[0].checked == true)
{
if(questions[store][0] == questions[store][5])
document.getElementById("ans").innerHTML = "Correct";
else
document.getElementById("ans").innerHTML = "Incorrect";
}
else if(radio[1].checked == true)
{
if(questions[store][1] == questions[store][5])
document.getElementById("ans").innerHTML = "Correct";
else
document.getElementById("ans").innerHTML = "Incorrect";
}
else if(radio[2].checked == true)
{
if(questions[store][2] == questions[store][5])
document.getElementById("ans").innerHTML = "Correct";
else
document.getElementById("ans").innerHTML = "Incorrect";
}
else if(radio[3].checked == true)
{
if(questions[store][3] == questions[store][5])
document.getElementById("ans").innerHTML = "Correct";
else
document.getElementById("ans").innerHTML = "Incorrect";
}
else
document.getElementById("ans").innerHTML = "Please select an answer!";
}
}
function startMenu()
{
window.history.back();
}