+ 1
[solved] I need help with this java script program
alert ("type in your name") var x = prompt() alert ("type in your age") var y = prompt() alert ("do you want to print? \ntype yes or no") var z = prompt() if (z == "yes") { document.write ("name: " + x) document.write ("age: " + y) if (y > 18) { document.write("adult") } if (y <== 18) && (y > 12) { document.write("teenager") } if (y <== 12) && (y > 10) { document.write("tween") } if (y <== 10) { document.write("child") } }
6 Answers
+ 2
Jonathan Cuppen
2 problems.
1. y <=.
y <== is invalid syntax
2. if(condition){statement}
Your synax is:
if (condition)(condition){statement}
alert ("type in your name")
var x = prompt()
alert ("type in your age")
var y = prompt()
alert ("do you want to print? \ntype yes or no")
var z = prompt()
if (z == "yes") {
document.write ("name: " + x)
document.write ("age: " + y)
if (y > 18) {
document.write("adult")
}
if (y <= 18 && y > 12) {
document.write("teenager")
}
if (y <= 12 && y > 10) {
document.write("tween")
}
if (y <= 10) {
document.write("child")
}
}
+ 1
I didnāt see the isolated equals sign instead of the double equals
+ 1
Thanks!
+ 1
Adding to Rik's answer ...
You don't have to display an alert box before reading user's input. The prompt() method accepts a string argument that shows when asking for input.
var uName = prompt("Type in your name:");
var uAge = Number(prompt("Type in your age:"));
Notice the use of Number() method above to try and convert given input into a numeric value so we can do numeric comparisons proportionally.
The prompt() method returns given input as a string, so a conversion from string to number is in order where we want to work with the input as a number.
0
I already tried that
0
Never mind