Skip to content

Instantly share code, notes, and snippets.

@oneEyedSunday
Created July 2, 2022 22:45
Show Gist options
  • Save oneEyedSunday/130fb27efcc96edbfa03c126744c3ed3 to your computer and use it in GitHub Desktop.
Save oneEyedSunday/130fb27efcc96edbfa03c126744c3ed3 to your computer and use it in GitHub Desktop.
A walkthrough loops and arrays for beginners
// declare function
// function <name> (firstParameter secondParameter, ...otherParaemters)
// Notice the `...` before otherParameters
// It means it can either be zero or more entries
// These entries will be collapsed into an array called whatever you named the parameter
function walkthrough(param1, param2, ...otherVariadicParams) {
// initialize sum to 0
let sum = 0;
// Here, this iteration varaibale is so you can track how many times the loop happened
// It can be different from the index of the loop itself
// In our case we dont start our index from zero always;
// It depends on user input
// So to visualize how many times the loop runs, this is helpful
let iteration = 0;
// the index of the loop is defined here
// the index = param1 means the index strats from the value of the first parameter
// the `index < param2` means, keep looping as long as index is less than value of param2
// if youre looping through an array
// this will usually be written as `index < array.length`
// since you want to loop till the end of the array
// and start from beginning of array
// if for instance you wanted to loop from end of array to begginning of array
// You'd write the loop as
// for (let index = array.length - 1; index <= 0; index--)
// meaning start from end of array (array.length - 1); since an array is indexed from 0 till array.length - 1
// keep looping till the index reaches 0
// reduce index by 1 on every iteration
// since we want to walk backwards
// e.g array [0, 1, 2] (using 0... so its easier to keep up with indexes)
// start loop from end of array (length = 3)
// so start loop at index (3 - 1) sp array[2] (which is 2 here)
// loop till beginning of array; index = 0; array[0]
// reduce index by 1 on every iteration
// so array[2], array[1], array[0]
// Also here; we are increasing the index by 2
// Not by 1 which is usually the default (index++)
for (let index = param1; index < param2; index += 2) {
console.log("Debug: iteration #", iteration, " Sum at beginning of iteration is: ", sum, " Index of loop is: ", index, " value of array at loop index is: ", otherVariadicParams[index]);
// here because the index may be greater than the length / bounds of the array
// and array[numberLargerTahnLength] will be undefined
// we do the null check
// so if its undefined we turn it to 0
// array[bigNUmber] || 0
// basically means
// if (!array[bigNUmber])
// if (array[bigNUmber] === undefined)
// if (array[bigNUmber] === null)
// return 0
// We are doing this since undefined + 3
// undefined + number = NaN (not a number)
sum = sum + (otherVariadicParams[index] || 0);
// we have to manulaly increase the iteration variable
// since the loop only increase the index automaticallt
// although if we wrote the loop as
// for (let index = param1; index < param2; index += 2, iteration++)
// Notice the comma above between `index +=2` and `iteration++`
iteration = iteration + 1;
}
// Here we log the value of the array we are working on,
// the sum
// and the parameters for debug purposes
console.log('Debug: ', 'Array is: ', otherVariadicParams, ' Sum from: ', param1, " to: ", param2, "is: ", sum);
return sum;
}
// call function
console.log('Result is: ', walkthrough(5, 20));
console.log(" ======================== ")
walkthrough(1, 5, 3, 4)
console.log(" ======================== ")
walkthrough(1, 5, 3, 4, 5, 6, 7, 8)
console.log(" ======================== ")
walkthrough(5, 5, 3, 4, 5)
console.log(" ======================== ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment