Skip to the content.

Iteration Popcorn Hacks

Iterations Popcorn Hacks

CSSE JavaScript Fundamentals

Popcorn Hacks on Iterations in JavaScript

Instructions:
Complete the exercises below in JavaScript.
You can run and test your code in a JavaScript environment.

Exercise 1: Counting with a For Loop

Write a JavaScript for loop that prints all numbers from 1 to 10.
Example:
Output:
1
2
3
9
10
%%js
for ( let count = 1; count < 11; count++) { //making numbers from 1 to 10
    console.log(count) //printing count
}
<IPython.core.display.Javascript object>

Exercise 2: Sum of Numbers

Write a function in JavaScript to calculate the sum of all numbers from 1 to n using a loop.
Example:
Input: 5
Output: 15 (because 1 + 2 + 3 + 4 + 5 = 15)
%%js
let total = 0 //defining total
for (let count = 0; count <= 5; count++ ) { //makes numbers from 1 to 5
    total += count //every time a number is made it is added to the total
}
console.log(total) //print total
<IPython.core.display.Javascript object>

Exercise 3: Looping through Arrays

Write a JavaScript program to iterate through an array of names and print each name.
%%js
const names = ['dumbmist','evan','yomama','me','you'] //making a list of names

for ( let i = 0; i < names.length; i++ ) { // iterates for the lentgh of the names
    console.log(names[i]) //print the names
}

<IPython.core.display.Javascript object>
%%js
const names = ['dumbmist','evan','yomama','me','you'] //makes a list of names

for (let name in names) { //for every 1 name in the list of names
    console.log(name) //prints names
}
<IPython.core.display.Javascript object>