Name:___________________

Looping Through a List

Generally, accessing each element in a list is tedious and impractical.

Instead you can loop through a list using a for loop:

var list = [4, 6, 7, 4, 10];

function sumList(){
 
for(var i = 0; i < list.length; i++){
     
//do something with list[i];
  }
}

Practice

For each code chunk below, write down the expected output in the console.

1.

var list = ["Jim", 6, "Strawberry", 6.7, "Music"];

for(var i = 0; i < list.length; i++){
   
console.log(list[i]);    
}

console


2.

var list = ["Jim", 6, "Strawberry", 6.7, "Music"];

for(var i = list.length-1; i >=0; i--){
   
console.log("item "+i+": "list[i]);    
}

console


Challenge

Given an list of numbers:

var nums = [10, -12, 6, 75, -15, 22];

Can you write a script that will find the maximum (and as a bonus, the minimum) of all the value in the list or any other list of numbers?

minmax.js