I'm attempting to parse an integer from two prompts (that will take a string) and add the two answers together. So far, the prompts work, but nothing is being written in the document like expected. Can you let me know why my code isn't displaying properly?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Chapter 2, Example 6</title>
</head>
<body>
<script>
var firstNumber = prompt("Enter the first number");
var secondNumber = prompt("Enter the second number");
var theTotal = firstNumber.parseInt() + secondNumber.parseInt();
document.write(firstNumber + "added to " + secondNumber + " equals" + theTotal);
</script>
</body>
</html>
window.prompt() returns a string.
parseInt() is not a method of the String prototype but rather a built-in function that accepts the string to parse.
Update this line:
var theTotal = firstNumber.parseInt() + secondNumber.parseInt();
To:
var theTotal = parseInt(firstNumber) + parseInt(secondNumber);
Also, to avoid confusion for readers and guarantee predictable behavior, pass the second parameter (i.e. radix) - presumably 10 for base-10 numbers.
var theTotal = parseInt(firstNumber, 10) + parseInt(secondNumber, 10);
var firstNumber = prompt("Enter the first number");
var secondNumber = prompt("Enter the second number");
var theTotal = parseInt(firstNumber, 10) + parseInt(secondNumber, 10);
console.log(firstNumber + " added to " + secondNumber + " equals: " + theTotal);