Skip to content

Instantly share code, notes, and snippets.

@iancover
Created April 24, 2017 09:57
Show Gist options
  • Save iancover/0c3bb689fae9d2f9b6b707151cd98c64 to your computer and use it in GitHub Desktop.
Save iancover/0c3bb689fae9d2f9b6b707151cd98c64 to your computer and use it in GitHub Desktop.
Loop Drills
// Compute Average Drill
function average(numbers) {
var avg = numbers.reduce(function sum(total,num) {
return total + num;
})
return avg/numbers.length;
}
// I used the .reduce() method with .length
// Curriculum Solution
function average(numbers) {
var total = numbers[0];
for (var i=1; i < numbers.length; i++) {
total+= numbers[i];
}
return total/number.length;
}
// 1. Set the value of the first number.
// 2. Then add each of the remaining numbers to total.
// 3. Return the total divided by the length of array.
// FizzBuzz Drill
function fizzBuzz(countTo) {
var currentNum = countTo[0];
var div3 = num % 3 === 0;
var div5 = num % 5 === 0;
for (var i=0; i<=countTo.length; i++);
return countTo.map(function(sub) {
if (countTo[i] = div3) {
currentNum = "fizz";
}
if (countTo[i] = div5) {
currentNum = "buzz";
}
if (countTo[i] = div3 && div5) {
currentNum = "fizzbuzz";
}
})
}
// Couldnt figure it out, was using .map() method
// Solution
function fizzBuzz(countTo) {
var result = [];
for (var i=1; i <= countTo; i++) {
if (i % 15 === 0) {
result.push('fizzbuzz');
}
else if (i % 5 === 0) {
result.push('buzz');
}
else if (i % 3 === 0) {
result.push('fizz');
}
else {
result.push(i);
}
}
return result;
}
// 1. Create an empty array that will contain results
// 2. Count from 1 to `countTo`, and for each number, add the number or its substitution (fizz, buzz, fizzbuzz)
// to the result array.
// Max/Min without Sort Drill
// Create two functions, one to return the max number on a list and a second one to return the min
function max(numbers) {
for (var i=0; i <= numbers; i++);
return numbers.find(max);
}
function min(numbers) {
// your code here
}
// Couldnt solve at 20 mins
// 'numbers[i]' tells the function what ever position 'i' is at, if that value is > (greater than), then set as currentMax
// Solution
function max(numbers) {
var currentMax = numbers[0];
for (var i=0; i <= numbers.length; i++) {
if (numbers[i] > currentMax) {
currentMax = numbers[i];
}
}
return currentMax;
}
// 1. Set 'currentMax' to the value of first item so we can have a number to compare it to
// 2. Loop through 'numbers' comparing each item to 'currentMax'
// 3. 'if' the item is greater than 'currentMax', set 'currentMax' to that number
// 4. return currentMax
function min(numbers) {
var currentMin = numbers[0];
for (var i=0; i <= numbers.length; i++) {
if (numbers[i] < currentMin) {
currentMin = numbers[i];
}
}
return currentMin;
}
// 1. Set 'currentMin' to the value of first item in 'numbers'
// 2. Loop through 'numbers' comparing each item to 'currentMin'
// 3. 'if' the item is less than 'currentMin', set 'currentMin' to that number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment