How do you give two alerts for "InA" and "YoP" in the second program to make both programs equal? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 4

How do you give two alerts for "InA" and "YoP" in the second program to make both programs equal?

// First program var age = prompt("Input your age"); var answer = 2019 - age; if (!age) { alert("Please enter a number") } else if (age > 120){ alert("InA"); alert("YoP"); } else { alert("You were born in " + answer); } // Second program let age = prompt("input your age"); let year = 2019 - age; let answer = `${!age ? "Please enter a number" : year > 120 ? "InA, YoP" : "You were born in " + year}` alert(answer);

4th Jul 2019, 6:54 PM
eMBee
eMBee - avatar
2 Réponses
+ 2
I made a few little changes to the second answer to make it show 2 alerts without using if-statements: let age = prompt("input your age"); let year = 2019 - age; let answers = !age ? ["Please enter a number"] : year > 120 ? ["InA", "YoP"] : ["You were born in " + year]; answers.forEach(alert); The changes include: - Replaced expression evaluating to a single string to one evaluating to an Array of string - renamed answer to answers to correspond with it potentially being multiple strings. - Replaced single alert with answers.forEach(alert) so alert would be called on each and every string. - I removed the `${ characters because the template literal doesn't seem helpful there.
6th Jul 2019, 10:58 PM
Josh Greig
Josh Greig - avatar
+ 2
Thanks Josh Greig
7th Jul 2019, 4:40 AM
eMBee
eMBee - avatar