0

I'm trying to get the add() function to calculate in the if-else statement and not the line commented out for part of an assignment but I'm not sure how to get it to show the result in a new window.

function add(){
   return +num1 + +num2
};

const num1 = window.prompt("Enter a number: ");
const operatorInput = window.prompt("Do you want to add, subtract, multiply, or divide?");
let num2;

if (operatorInput === "add" || operatorInput === "Add") {
    num2 = window.prompt("Second Number: ");
    // add()
    // let sum= parseInt(num1) + parseInt(num2)
    // alert(num1 + " " + "+" + " " + num2 + " " + "=" + " " + add());
    alert(`${num1} + ${num2} = ${add()}`);
};

4
  • show the result in a new window --> Using alert() would display a pop-up with the message. But that's not what you need. Instead, you require a new window - is that correct? Commented Apr 16, 2022 at 0:36
  • Yes, that's correct! Commented Apr 16, 2022 at 0:40
  • In that case, please refer to this documentation. Commented Apr 16, 2022 at 0:55
  • The question has been updated to fix bugs in the code (userInput v/s operatorInput, converting num1, and num2 to numbers, declaring num2 outside of the if, preferring to use const and let instead of var, the if condition corrected, etc), make it runnable within stack-snippets, and improve it slightly (using template literals, for example). Commented Apr 16, 2022 at 1:04

1 Answer 1

2

Not sure if that's what you need.

var num1 = parseFloat(window.prompt("Enter a number: "));
var operatorInput = window.prompt(
    "Do you want to add, subtract, multiply, or divide?"
);
while (
    operatorInput.toLowerCase() != "add" &&
    operatorInput.toLowerCase() != "multiply" &&
    operatorInput.toLowerCase() != "divide" &&
    operatorInput.toLowerCase() != "subtract"
) {
    alert("Invalid Input! Please try again");
    operatorInput = window.prompt(
        "Do you want to add, subtract, multiply, or divide?"
    );
}
var num2 = parseFloat(window.prompt("Second Number: "));
var resultWindow = window.open("", "Result Window", "width=200,height=200");
if (operatorInput.toLowerCase() == "add")
    resultWindow.document.write(
        `<p>${num1 + " " + "+" + " " + num2 + " " + "=" + " " + (num1 + num2)
        }</p>`
    );
else if (operatorInput.toLowerCase() == "multiply")
    resultWindow.document.write(
        `<p>${num1 + " " + "*" + " " + num2 + " " + "=" + " " + num1 * num2
        }</p>`
    );
else if (operatorInput.toLowerCase() == "divide")
    resultWindow.document.write(
        `<p>${num1 + " " + "/" + " " + num2 + " " + "=" + " " + num1 / num2
        }</p>`
    );
else
    resultWindow.document.write(
        `<p>${num1 + " " + "-" + " " + num2 + " " + "=" + " " + (num1 - num2)
        }</p>`
    );

You should be careful with using new windows to display information, it's not recommended and many browsers now adays automatically blocks these actions and consider them as spam popup windows if they're directly invoked by javascript. If the results are not being shown for you make sure to allow your browser to allow popups from the link you're displaying it from.

Sign up to request clarification or add additional context in comments.

2 Comments

Good answer. +10. An observation: Since backtick / template-literat is already used to enclose the p tags, if you like, you could simplify the document.write like so: document.write(`<p>${num1} + ${num2} = ${num1 + num2}</p>`).
Yes you're right! Thanks for pointing that out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.