- start a new html document
- Inside the body, put your name inside an H1 tag
- Add a script tag:
<script>
</script>
- Declare a variable named answer
- Use the prompt() function to get a value for answer
answer=prompt("Enter a number", "0");
- alert the answer:
- alert (answer);
- Get a second number
- Write the second number to the screen using document.write
value2=prompt("enter another number", "0");
document.write (value2);
- alert the sum of the 2 numbers
- alert("the answer is" + answer+value2)
- What's wrong??
- You can't mix strings and numbers
- Try the following code to see what is happening
alert (parseInt(answer)+parseInt(value2));
alert ("answer:" + parseInt(answer)+parseInt(value2));
- Now, lets fix the code to work the way we want
sum= parseInt(answer)+parseInt(value2);
alert ("answer:" + sum);