+ 1

JavaScript Function Movie Lab Age Conditional Statement

Can someone come up with a Javascript solution to this problem.... According to the description of R-rated films: Children under 17 require an accompanying parent or adult guardian (age 21 or older) and adults 25 years and under must show ID. And children under the age of 6 are not allowed in after 6:00pm. Deadpool is an R-rated movie. Write a JavaScript function named canIWatch that will take age as a parameter. If the age is less than 6, return You are not allowed to watch Deadpool after 6.00pm. If the age is 6 or more but less than 17, return You must be accompanied by a guardian who is 21 or older. If the age is 17 or more but less than 25, return You are allowed to watch Deadpool, right after you show some ID. If the age is 25 or greater, return Yay! You can watch Deadpool with no strings attached!. If the age is invalid, return Invalid age.

21st Mar 2017, 1:40 PM
Samuel Mwanyasi
Samuel Mwanyasi - avatar
2 Answers
0
Thank you.... I figured it out and came with the code... plus its HTML document.. <!DOCTYPE html> <html> <body> <p>Click the button to See if you Can Watch DeadPol Movie</p> <button onclick="canIWatch()">Click To See If You Can See</button> <p id="demo"></p> <script> function canIWatch() { var age = prompt("Enter Your Age Please!"); if (age < 6) { output = "You are not allowed to watch Deadpool after 6.00pm."; } else if (age < 17) { output = "You must be accompanied by a guardian who is 21 or older."; } else if (age < 25) { output = "You are allowed to watch Deadpool, right after you show some ID."; }else if (age > 25) { output = "Yay! You can watch Deadpool with no strings attached!"; }else { output = "Invalid age."; } document.getElementById("demo").innerHTML = output; } </script> </body> </html>
2nd Apr 2017, 6:02 PM
Samuel Mwanyasi
Samuel Mwanyasi - avatar
+ 17
var age = prompt ("What'a your age?"); var date = new Date(); var hours = date.getHours(); if (age < 6 && age > 0 && hours >= 6.00){ document.write("You are not allowed to watch Deadpool after 6.00pm."); } else if (age > 6 && age < 17){ document.write("You must be accompanied by a guardian who is 21 or older."); } else if (age > 17 && age <25){ document.write ("You are allowed to watch Deadpool, right after you provide some ID."); } else if (age > 25){ document.write ("Yay! You can watch Deadpool with no string attachment."); } //The last is else statement for some people without mind whow would try to write their age as -1 or something else. else { document.write ("Really?"); } //Inform me if I have commited some mistake.
2nd Apr 2017, 5:55 PM
Biraj Patel