Tutorial 12#
== Functions & if / else ==
== Functions & if / else ==
An especially useful application of reusable code is
We are going to write a function that checks how many hours of sleep a night you're getting. Inside the function will be an
Instructions:
var sleepCheck= function(numHours)
{
sleepCheck(8);
if(numHours >= 8)
{
return console.log("You're getting plenty of sleep! Maybe even too much!");
}
else {
return console.log("Get some more shut eye!");
}
};
if
/else
statements. These can be very wordy, and a pain to type repeatedly.We are going to write a function that checks how many hours of sleep a night you're getting. Inside the function will be an
if
/else
statement. We want the function to check many different numbers of hours to see whether a person is getting enough sleep.Instructions:
- Write a function named
sleepCheck
that takes the parameternumHours
- Inside the function, write an
if
statement where if the number of hours of sleep is greater than or equal to 8, the computer willreturn
"You're getting plenty of sleep! Maybe even too much!";
. - Otherwise (
else
) if the number of hours of sleep is less than 8, have the computerreturn
"Get some more shut eye!";
- Call the function with 10 hours of sleep, like this:
sleepCheck(10);
- Call the function with 5 hours of sleep.
- Call the function with 8 hours of sleep.
var sleepCheck= function(numHours)
{
sleepCheck(8);
if(numHours >= 8)
{
return console.log("You're getting plenty of sleep! Maybe even too much!");
}
else {
return console.log("Get some more shut eye!");
}
};
No comments:
Post a Comment