result appearing twice in calculator - javascript

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.

Related

Javascript: Making a functional calculator

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
}
}

JavaScript: print the ticketPrice and set the ticketPrice variable

var ticketType;
ticketType = prompt("What sort of tickets would you like");
document.write("Ticket Type Cost is: " + ticketType);
document.write("<br/>");
if (ticketType == "A") {
document.write("$100");
}
else if (ticketType == "a") {
document.write("$100");
}
else if (ticketType == "B") {
document.write("$75");
}
else if (ticketType == "b") {
document.write("$75");
}
else if (ticketType == "C") {
document.write("$50");
}
else if (ticketType == "c") {
document.write("$50");
}
else {
document.write("Invalid ticket type");
}
var ticketQty;
ticketQty = prompt("How many tickeets would you like");
var ticketQty = parseInt(ticketQty);
document.write("<br/>");
document.write("Ticket Qty is: " + ticketQty);
document.write("<br/>");
if (ticketQty < 0 || ticketQty > 100) {
document.write("Invalid Qty");
}
var ticketPrice = ;
if (ticketPrice > 0 && ticketQty > 0 && ticketQty < 100) {
document.write("Ticket payment required is:$" + (ticketPrice*ticketQty));
}
I need help with this last variable 'ticketPrice'. I have tried a lot of different things but none of them seem to work. What should I set my ticketPrice variable to and do i need to add anything else to my code or remove anything in order to make it work properly.
You would want to set the ticketPrice variable inside your if statements when you know the price. So you should define the var ticketPrice at the top and in each if statement where you are doing document.write also assign the variable.
var ticketType;
var ticketPrice;
ticketType = prompt("What sort of tickets would you like");
document.write("Ticket Type Cost is: " + ticketType);
document.write("<br/>");
if (ticketType == "A") {
document.write("$100");
ticketPrice = 100;
}
else if (ticketType == "a") {
document.write("$100");
ticketPrice = 100;
}
else if (ticketType == "B") {
document.write("$75");
ticketPrice = 75;
}
else if (ticketType == "b") {
document.write("$75");
ticketPrice = 75;
}
else if (ticketType == "C") {
document.write("$50");
ticketPrice = 50;
}
else if (ticketType == "c") {
document.write("$50");
ticketPrice = 50;
}
else {
document.write("Invalid ticket type");
}
var ticketQty;
ticketQty = prompt("How many tickeets would you like");
var ticketQty = parseInt(ticketQty);
document.write("<br/>");
document.write("Ticket Qty is: " + ticketQty);
document.write("<br/>");
if (ticketQty < 0 || ticketQty > 100) {
document.write("Invalid Qty");
}
if (ticketPrice > 0 && ticketQty > 0 && ticketQty < 100) {
document.write("Ticket payment required is:$"
+ (ticketPrice*ticketQty));
}
You should also consider refactoring this to reduce some of the duplication around your if statements
if (ticketType == "A") {
document.write("$100");
ticketPrice = 100;
}
else if (ticketType == "a") {
document.write("$100");
ticketPrice = 100;
}
else if (ticketType == "B") {
document.write("$75");
ticketPrice = 75;
}
else if (ticketType == "b") {
document.write("$75");
ticketPrice = 75;
}
else if (ticketType == "C") {
document.write("$50");
ticketPrice = 50;
}
else if (ticketType == "c") {
document.write("$50");
ticketPrice = 50;
}
else {
document.write("Invalid ticket type");
}
Could become
if (ticketType.toLowerCase() === "a") {
document.write("$100");
ticketPrice = 100;
} else if (ticketType.toLowerCase() === "b") {
document.write("$75");
ticketPrice = 75;
} else if (ticketType.toLowerCase() === "c") {
document.write("$50");
ticketPrice = 50;
} else {
document.write("Invalid ticket type");
}
Reducing duplication here would make it slightly easier to update the prices of your tickets for example as you wouldn't need to change it in 2 places (the uppercase and lowercase blocks)
Change ticketPrice = ; to ticketPrice; as it produce error.
var ticketType;
ticketType = prompt("What sort of tickets would you like");
document.write("Ticket Type Cost is: " + ticketType);
document.write("<br/>");
if (ticketType == "A") {
document.write("$100");
}
else if (ticketType == "a") {
document.write("$100");
}
else if (ticketType == "B") {
document.write("$75");
}
else if (ticketType == "b") {
document.write("$75");
}
else if (ticketType == "C") {
document.write("$50");
}
else if (ticketType == "c") {
document.write("$50");
}
else {
document.write("Invalid ticket type");
}
var ticketQty;
ticketQty = prompt("How many tickeets would you like");
var ticketQty = parseInt(ticketQty);
document.write("<br/>");
document.write("Ticket Qty is: " + ticketQty);
document.write("<br/>");
if (ticketQty < 0 || ticketQty > 100) {
document.write("Invalid Qty");
}
var ticketPrice;
if (ticketPrice > 0 && ticketQty > 0 && ticketQty < 100) {
document.write("Ticket payment required is:$" + (ticketPrice*ticketQty));
}
Assign the value for ticketPrice in your set of if statements, and declare it ahead of time.
var ticketType;
var ticketPrice;
ticketType = prompt("What sort of tickets would you like");
document.write("Ticket Type Cost is: " + ticketType);
document.write("<br/>");
if (ticketType == "A") {
ticketPrice=100;
}
else if (ticketType == "a") {
ticketPrice=100;
}
else if (ticketType == "B") {
ticketPrice=75;
}
else if (ticketType == "b") {
ticketPrice=$75;
}
else if (ticketType == "C") {
ticketPrice=50;
}
else if (ticketType == "c") {
ticketPrice=50;
}
else {
document.write("Invalid ticket type");
}
if (typeof ticketPrice !== "undefined" && ticketPrice !== null)
{
document.write("$");
document.write(ticketPrice);
}
var ticketQty;
ticketQty = prompt("How many tickeets would you like");
var ticketQty = parseInt(ticketQty);
document.write("<br/>");
document.write("Ticket Qty is: " + ticketQty);
document.write("<br/>");
if (ticketQty < 0 || ticketQty > 100) {
document.write("Invalid Qty");
}
var ticketPrice = ;
if (ticketPrice > 0 && ticketQty > 0 && ticketQty < 100) {
document.write("Ticket payment required is:$" + (ticketPrice*ticketQty));
}

Why is the else statement executed after the if when the if is true?

The following codeļ¼š
var aa = 1;
if (aa == 1){
console.log("true")
}
else (aa == 2)
{
console.log("false")
}
prints:
true
false
please tell me why?
With proper formatting, your code is actually this:
var aa = 1;
if (aa == 1) {
console.log("true")
} else {
(aa == 2);
}
{
console.log("false")
}
The last block with the log statement is not associated with the if..else at all. You're missing the if in else if.
this is not valid if statement see
var aa = 1;
if (aa == 1){
console.log("true")
}//see next line you miss if after else
else if(aa == 2)
{
console.log("false")
}
You have 'mistakes' in code, your code executes as
var aa = 1;
if (aa == 1) {
console.log("true")
} else {(aa == 2)}
{
console.log("false")
}
and right code is
var aa = 1;
if (aa == 1) {
console.log("true")
} else if (aa == 2) {
console.log("false")
}

This JavaScript code is not working. Have I got the syntax wrong or is it something else?

This code is not working as I would have liked it to, but I am not sure of the problem.
Code:
<body>
var randNumForQuote = Math.floor((Math.random() * 11));
if (randNumForQuote == 0) {
document.getElementById("quoteDiv").innerHTML = "Hello";
} else if (randNumForQuote == 1) {
document.getElementById("quoteDiv").innerHTML = "Hello1";
} else if (randNumForQuote == 2) {
document.getElementById("quoteDiv").innerHTML = "Hello2";
} else if (randNumForQuote == 3) {
document.getElementById("quoteDiv").innerHTML = "Hello3";
} else if (randNumForQuote == 4) {
document.getElementById("quoteDiv").innerHTML = "Hello4";
} else if (randNumForQuote == 5) {
document.getElementById("quoteDiv").innerHTML = "Hello5";
} else if (randNumForQuote == 6) {
document.getElementById("quoteDiv").innerHTML = "Hello6";
} else if (randNumForQuote == 7) {
document.getElementById("quoteDiv").innerHTML = "Hello7";
} else if (randNumForQuote == 8) {
document.getElementById("quoteDiv").innerHTML = "Hello8";
} else if (randNumForQuote == 9) {
document.getElementById("quoteDiv").innerHTML = "Hello9";
} else if (randNumForQuote == 10) {
document.getElementById("quoteDiv").innerHTML = "Hello10";
}
<div id="quoteDiv"></div>
</body>
I have limited experience in JavaScript, so do not understand the problem too well.
I am expecting the div to say one of the outputs (eg. Hello, Hello1, Hello2, etc.)
You need to put the JavaScript code inside script tag and run the code after page is loaded so put inside window.onload callback, which fires at the end of the document loading process.
<body>
<script>
window.onload = function() {
var randNumForQuote = Math.floor((Math.random() * 11));
if (randNumForQuote == 0) {
document.getElementById("quoteDiv").innerHTML = "Hello";
} else if (randNumForQuote == 1) {
document.getElementById("quoteDiv").innerHTML = "Hello1";
} else if (randNumForQuote == 2) {
document.getElementById("quoteDiv").innerHTML = "Hello2";
} else if (randNumForQuote == 3) {
document.getElementById("quoteDiv").innerHTML = "Hello3";
} else if (randNumForQuote == 4) {
document.getElementById("quoteDiv").innerHTML = "Hello4";
} else if (randNumForQuote == 5) {
document.getElementById("quoteDiv").innerHTML = "Hello5";
} else if (randNumForQuote == 6) {
document.getElementById("quoteDiv").innerHTML = "Hello6";
} else if (randNumForQuote == 7) {
document.getElementById("quoteDiv").innerHTML = "Hello7";
} else if (randNumForQuote == 8) {
document.getElementById("quoteDiv").innerHTML = "Hello8";
} else if (randNumForQuote == 9) {
document.getElementById("quoteDiv").innerHTML = "Hello9";
} else if (randNumForQuote == 10) {
document.getElementById("quoteDiv").innerHTML = "Hello10";
}
}
</script>
<div id="quoteDiv"></div>
</body>
Although you ca reduce the code
<body>
<script>
window.onload = function() {
var randNumForQuote = Math.floor((Math.random() * 11));
document.getElementById("quoteDiv").innerHTML = "Hello" + (randNumForQuote ? " " + randNumForQuote : '');
}
</script>
<div id="quoteDiv"></div>
</body>
Javascript needs to be in a script tag if it's in html, and you need to have the div come before the script tag.
<body>
<div id="quoteDiv"></div>
<script>
var randNumForQuote = Math.floor((Math.random() * 11));
if (randNumForQuote == 0) {
document.getElementById("quoteDiv").innerHTML = "Hello";
} else if (randNumForQuote == 1) {
document.getElementById("quoteDiv").innerHTML = "Hello1";
} else if (randNumForQuote == 2) {
document.getElementById("quoteDiv").innerHTML = "Hello2";
} else if (randNumForQuote == 3) {
document.getElementById("quoteDiv").innerHTML = "Hello3";
} else if (randNumForQuote == 4) {
document.getElementById("quoteDiv").innerHTML = "Hello4";
} else if (randNumForQuote == 5) {
document.getElementById("quoteDiv").innerHTML = "Hello5";
} else if (randNumForQuote == 6) {
document.getElementById("quoteDiv").innerHTML = "Hello6";
} else if (randNumForQuote == 7) {
document.getElementById("quoteDiv").innerHTML = "Hello7";
} else if (randNumForQuote == 8) {
document.getElementById("quoteDiv").innerHTML = "Hello8";
} else if (randNumForQuote == 9) {
document.getElementById("quoteDiv").innerHTML = "Hello9";
} else if (randNumForQuote == 10) {
document.getElementById("quoteDiv").innerHTML = "Hello10";
}
</script>
</body>
<html>
<body>
<div id="quoteDiv"></div>
</body>
<script>
// self executing function here
var randNumForQuote = Math.floor((Math.random() * 11));
if (randNumForQuote == 0) {
document.getElementById("quoteDiv").innerHTML = "Hello";
} else if (randNumForQuote == 1) {
document.getElementById("quoteDiv").innerHTML = "Hello1";
} else if (randNumForQuote == 2) {
document.getElementById("quoteDiv").innerHTML = "Hello2";
} else if (randNumForQuote == 3) {
document.getElementById("quoteDiv").innerHTML = "Hello3";
} else if (randNumForQuote == 4) {
document.getElementById("quoteDiv").innerHTML = "Hello4";
} else if (randNumForQuote == 5) {
document.getElementById("quoteDiv").innerHTML = "Hello5";
} else if (randNumForQuote == 6) {
document.getElementById("quoteDiv").innerHTML = "Hello6";
} else if (randNumForQuote == 7) {
document.getElementById("quoteDiv").innerHTML = "Hello7";
} else if (randNumForQuote == 8) {
document.getElementById("quoteDiv").innerHTML = "Hello8";
} else if (randNumForQuote == 9) {
document.getElementById("quoteDiv").innerHTML = "Hello9";
} else if (randNumForQuote == 10) {
document.getElementById("quoteDiv").innerHTML = "Hello10";
}
</script>
</html>

Check if last character is a question mark in javascript

function askQuestion(){
var Prompt = prompt("What yes or no question do you have?", "Type it here...");
var number = Math.floor((Math.random() * 8) + 1);
if(Prompt != null){
if (number == 1){
alert("Signs point yo yes.");
}else if(number == 2){
alert("Yes.");
}else if(number == 3){
alert("Reply hazy, try agian.");
}else if(number == 4){
alert("Doubtful.");
}else if(number == 5){
alert("All signs point to no.");
}else if(number == 6){
alert("Most Likely.");
}else if(number == 7){
alert("Absolutely.");
}else if(number == 8){
alert("It doesn't look good.");
}
}else{
alert("Please re-ask the Magic 8 Ball.")
}
}
<body bgColor="Black">
<center><img src="8ball.png" onClick="askQuestion()" style="cursor:pointer;"></center>
</body>
This is what I have. What I would like to know, is how to see the text typed in the prompt has a question mark at the end.
if Prompt is a string then it should just be as simple as
var lastChar = Prompt.slice(-1);
if(lastChar == '?') { .... }
if (Prompt.slice(-1) === "?") {
...
}
Old answer (substr()):
var lastChar = (Prompt.trim().substr(-1) === '?')
New answer (substring()):
var lastChar = (Prompt.trim().substring(Prompt.length - 1) === '?')
You can use the charAt() method:
var lastChar = Prompt.charAt(Prompt.length-1);
if (lastChar === "?") {
// your logic here
}

Categories