Learning Objectives
- Describe while loops
- Use while loops
- Describe and identify functions
- Write functions in JavaScript
- Identify function calls
- Differentiate between function calls and function arguments
- Local Vs global scope
- Block scope
While Loop- Structure
As long as the condition is true, code is executed. When break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.
While Loop- Structure
- when the computer reaches the while statement, it tests to see if the boolean expression is true. If true, it jumps into the block of code immediately below the while statement.
- when the computer reaches the last line in the while loops block of code, it jumps back up to the while statement. It retest the boolean expression. If still true, it enters the block of code again.
- When the boolean expression becomes false, the computer skips the while statement and continues with the remainder of the program.
Click below for description of while loop
While Loop- syntax
The following is an example of a while loop that iterates a series of numbers as long as it is less than 10. It is adding +2 to the var a after every iteration.
Note: the print statement is python syntax.
While Loop- syntax
The following is an example of a While loop that iterates a series of numbers as long as it is less than 7. It is also running an if statement to check if the var a if divisible by 2 with no remainder and printing it with a string “ is even” else “is odd”
While Loop- Example
- Use loops when you need to run the same code as long as a condition is true
- Write a while loop to count down to 0
- Starting anywhere between 5 and 10
- What would be the condition the count down needs to be true?
- Number greater than 10?
- Number less than 10?
- Number greater than 0?
- What do you think?
Syntax
Initialize your variable:
var i=1;
while(Condition){
console.log(“Hello”)
i ++;
}
Exercise
Keep printing integers in multiples of 3 as long as integers are less than 35 using a while loop
Exercise
Keep printing integers in multiples of 5 as long as integers are less than 100
Exercise
Using a while loop print integers between 0 and 20. All numbers divisible by 2 should be multiplied by 3 before they are output. All other integers should not be output.
Hint: use Modulo
Exercise
Using a while loop, print out all prime numbers between 0 - 20
Bonus Logical Question
Nando went to the vending machine to buy himself a cookie. The cookie costs $4 dollars. He paid with a $10 bill, the vending machine paid him back in quarters. Write a loop that says how many quarters he got in return.
Functions
A block of code enclosed in braces { } Is used when same lines of code are being used multiple times throughout the program That block is declared as function using the right keyword
Functions cont.
If your goal was to add different combinations of two outputs of four variables x, y, z, w and add 3. one way is to add variables and add 3 every time it’s required.
(x+y+z+w)+3;
Second way is to write a function, which means write the code only once and use it as many times as required.
function add(x,y,z,w){
(x+y+z+w)+3;
}
add(4,5,6,7);
Which method saves time?
Functions in JavaScript
Functions in JS look like this is declared by the keyword function
- Keyword is followed by the name
-
Name is followed by the parameters
{ indicates the beginning of the block } indicates the end of the block
Function Calls
To use a function written in a file, it has to be called addition (4, 5 ); is a function call. A function call is needed to run a function written in the same file it includes the name of the function and parameters (if that function has arguments) this function, addition ( ), requires two values as arguments
Function Parameter and Function Argument
Function Parameters:
When a function is written, variables used in the definitions are the parameters. x and y are the parameters in the following image
Function Parameter and Function Argument
Function Argument
when a function is called, data that is passed to the function is the argument or arguments if I call the addition function for 6 and 8 like this addition (6, 8) 6 and 8 are the arguments.
Exercise
- Write a function that adds three numbers in ES5
- Write a function that adds three number and divide the result by 3 in ES5
Now try converting the code to arrow functions.
Scope –Global
Global scope of a variable, or Global variables are the variables declared in the program so that they can be accessed any time in any function.
Example Variable named global can be used inside the function
Scope – Local
Local variables can not be accessed outside of the function they are declared in.
Example Variable named local cannot be accessed outside the function
Bonus Logical Question
Let’s take the cookie quarter challenge and extend it to all possibilities.
What if the cookie cost $2 and I paid with a 20?
hint: incorporate parameters
Bonus Logical Question
Nando went to Uniqlo and bought himself a nice plaid shirt for $27.83. He paid with $100 as he had no change. The cashier paid returned his change in large denominations since they needed change for the other customers.
Write a loop that prints out how many bills he received in return in as large denominations as possible.
Fizz Buzz
Write a program that uses console.log to print all the numbers from 1 to 100 with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those).