Unit 5
Lesson 7 Loops Practice
To do: December 17
#1 What is the output of the following code snippet?
for(var i=0; i<5; i++)
{
var num = randomNumber(1,10);
console.log(num);
}
To do: December 17
#2 - How can you revise the code so that only even numbers are printed in the console?
for(var i=0; i<5; i++)
{
var num = randomNumber(1,10);
console.log(num);
}
To do: December 17
#2 - How can you revise the code so that only even numbers are printed in the console?
for(var i=0; i<5; i++)
{
var num = randomNumber(1,10);
if(num % 2 == 0)
{
console.log(num);
}
}
Go over homework
var steps = 0;
while(steps < 4){
moveForward();
steps++;
};
A for loop combines these three parts into one statement
for (var i=0; i<4; i++){
moveForward();
};
Any variable name can be used here. It’s most common to use i.
A loop is an example of iteration: a repetitive portion of an algorithm which repeats a specified number of times or until a given condition is met.
Unit 5 Lesson 6 - Activity
Lesson 7 Loops Practice
Unit 5 Lesson 7 - Activity
Debugging: the process of finding and fixing problems in code
Describe The Problem What do you expect it to do? What does it actually do? Does it always happen? | Hunt For Bugs Are there warnings or errors? What did you change most recently? Explain your code to someone else Look for code related to the problem |
Try Solutions Make a small change | Document As You Go What have you learned? What strategies did you use? What questions do you have? |
Unit 5 Lesson 7 - Activity
Debugging Loops
Use console.log to print out important information.
Track your iterator variable in the Watch panel.
Run your code slowly so you can see how your loop is actually working.
Unit 5 Lesson 7 - Activity
Loops Practice - U5L07 bubbles 1-8
Do This:
Class work due at the end of the day
Lists and Loops quiz on Tuesday, January 4