Beginner Javascript Problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Beginner Javascript Problem

Trying to store variables from three prompts, and give an alert with the output from the prompts. I keep getting an error that ")" is missing from line 8, but I cannot figure it out. //Can anyone tell me what is wrong with this? var user = prompt("Please enter your name"); var age = prompt("Please enter your age"); var gender = prompt('Please enter your gender\n male or female'); if (gender == "female") { alert('Hi'+ user ' are a '+age' old woman'); } else { alert('Hi'+ user ' are a '+age' old man'); }

25th May 2020, 3:49 PM
Stephen Keen
Stephen Keen - avatar
5 Answers
+ 4
Stephen Keen It should write like this. alert('Hi' + user + ' are a ' + age + ' old man'); Here user and age is a variable so you should write without quote and when you add two or more than two string you should write +
25th May 2020, 3:55 PM
A͢J
A͢J - avatar
+ 2
Stephen Keen Problem is in alert. You have Missed + after user in alert.
25th May 2020, 3:54 PM
A͢J
A͢J - avatar
+ 1
try this alert('Hi' + user + 'are a' + age + ' old woman'); alert('Hi' + user + 'are a' + age + ' old man'); or alert(`Hi ${user} are a ${age} old ${gender}`); alert(`Hi ${user} are a ${age} old ${gender}`);
25th May 2020, 4:02 PM
Emanuel Maliaño
Emanuel Maliaño - avatar
+ 1
Thank you so much!
25th May 2020, 4:10 PM
Stephen Keen
Stephen Keen - avatar
0
Hello Stephen Keen, the first error you had was that you were missing a + sign after the user variable. Remember that to concatenate it is like this: "text" + variable + "text" + variable + ... The other error I noticed is that when I put FeMaLe (uppercase) it went for the else statement. Which I solved with: gender = gender.toLowerCase(); To convert to lowercase and if compare as it should be. I leave the code fixed, I hope it works for you. https://code.sololearn.com/WP44N19d0mdg/?ref=app
25th May 2020, 4:23 PM
Josshual A. Toro M.
Josshual A. Toro M. - avatar