Skip to content

Instantly share code, notes, and snippets.

@booherbg
Last active Apr 22, 2020
Embed
What would you like to do?
Simple For Loops

Week 3 Stretch Goal Tips

In general there are two types of problems to solve:

  1. "Calculate some value based on all the numbers in the array"
  2. "Find a number in the array and stop once you've found it"

In pretty much all cases, all you need to do is have a way of looping through the array of numbers, keeping track of what you have done so far, and reporting the result at the end.

In some cases, you want to simply quit the for loop after you've found what you're looking for.

So here's a few examples. All of the problems can be solved using this general case (a for loop with a variable defined before the loop to keep track of your progress, and modified during the loop).

const numbersArray = [-1, 2, 15, -5, 7, 20];

// Find the sum of all numbers in the array
// first, create a variable to keep track of our progress
let sumSoFar = 0; // we start with 0

// second, loop through the array and add each number
// to the running total (represented by sumSoFar)
for (let i=0; i<numbersArray.length; i++) {
  let currentNumber = numbersArray[i]; // current number in the array
  sumSoFar = sumSoFar + currentNumber; // add the number to our running total
}
console.log('The sum of the array is: ' + sumSoFar);



// The second type of problem is one where we want to find
// the first number that meets a certain property
// once we find it, just break out of the loop!

// Find the first number that is evenly divisible by 5
// Remember that the modulus operator (%) is used to
// find the remainder after dividing one number by another.

// Loop through each number, check the remainder,
// and if its found to meet our criteria, log
// what we found and the loop!
for (let i=0; i<numbersArray.length; i++) {
  let currentNumber = numbersArray[i];
  // remainder will hold the left overs after dividing by 5
  // aka the 'remainder' (10 / 3 has a remainder of 1)
  let remainder = currentNumber % 5; 
  // if remainder is 0, we are evenly divisible by 5!
  if (remainder === 0) {
    // we found it! Report back then exit the loop
    console.log('The first number divisible by 5 is: ' + currentNumber);
    break;
  }
 }
 
 // What if we want to instead report the last number that met the
 // criteria, instead of the first?
 // Well, we can keep track of every number that met the criteria
 // by updating a variable, and instead of breaking out of the loop
 // we just let the loop run all the way through. Simply report
 // the value of the variable after the loop is finished!
 // It's kind of like a combination of the two examples above.
 
 let lastNumber = 'n/a';
 for (let i=0; i<numbersArray.length; i++) {
  let currentNumber = numbersArray[i];
  // remainder will hold the left overs after dividing by 5
  // aka the 'remainder' (10 / 3 has a remainder of 1)
  let remainder = currentNumber % 5; 
  // if remainder is 0, we are evenly divisible by 5!
  if (remainder === 0) {
    // we found it! Store what we found so far but keep looking
    lastNumber = currentNumber;
    // do not break! 
  }
 }
 // after the loop is done, lastNumber will hold 
 // the last number we found before the loop ended
 console.log('The last number divisible by 5 is ' + lastNumber);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment