From the course: Computer Science Principles Lab: JavaScript
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Use while loops - JavaScript Tutorial
From the course: Computer Science Principles Lab: JavaScript
Use while loops
- [Instructor] In JavaScript, we can create a while loop using the while statement. In a while loop, we need to create a condition that needs to remain true for the loop to execute. First, let's create a value to test within the loop. Our loop will go through each integer from zero to 99 and find all numbers that are evenly divisible by nine. Let's create our new variable. Var myValue is equal to zero. We first start with the while statement. Then, we create our condition test that will need to be true for the loop to continue. Since we're testing for all values below a hundred, we can enter that in as myValue is less than 100. Then we create our code block for the loop. Inside of this, we are going to use a new mathematical operator called the modulo operator. Modulo takes a value and divides it by another number. If there is a remainder, it returns that number. So for instance, if we tried to divide nine by nine, it divides evenly, so the result is zero. If we tried to divide 10 by…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Capture input from the user9m 17s
-
(Locked)
Manage different variable types6m 43s
-
(Locked)
Create conditional tests12m 16s
-
(Locked)
Build compound conditional tests8m 42s
-
(Locked)
Use while loops4m 13s
-
(Locked)
Use for loops7m 35s
-
Create arrays to store groups of values3m 36s
-
(Locked)
Use loops with arrays3m 26s
-
(Locked)
Changing arrays3m
-
(Locked)
-