Thursday, 26 June 2014

Tutorial 12#
== Functions & if / else ==

An especially useful application of reusable code is 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:
  1. Write a function named sleepCheck that takes the parameter numHours
  2. Inside the function, write an if statement where if the number of hours of sleep is greater than or equal to 8, the computer will return "You're getting plenty of sleep! Maybe even too much!";.
  3. Otherwise (else) if the number of hours of sleep is less than 8, have the computer return "Get some more shut eye!";
Then call the function with different hours of sleep
  1. Call the function with 10 hours of sleep, like this: sleepCheck(10);
  2. Call the function with 5 hours of sleep.
  3. Call the function with 8 hours of sleep.
Solve:
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