Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save escottalexander/64fd837ecf9498cefba837f25b063df3 to your computer and use it in GitHub Desktop.
Save escottalexander/64fd837ecf9498cefba837f25b063df3 to your computer and use it in GitHub Desktop.
https://repl.it/@ElliottAlexande/min-and-max-without-sort-drill
https://repl.it/@ElliottAlexande/average-drill
https://repl.it/@ElliottAlexande/fizzbuzz-drill-js
@Jefftopia
Copy link

Jefftopia commented May 2, 2018

https://repl.it/@ElliottAlexande/min-and-max-without-sort-drill

I'm totally violating the instructions, but here's a fun option:

const myArray = Array.from('1234567890');

Math.max(...myArray);
Math.max.apply(null, myArray);

Notice that Math.max implicitly attempts type conversion, from strings to numbers.

https://repl.it/@ElliottAlexande/fizzbuzz-drill-js

Your solution is great! But there's room for a small improvement.

function fizzBuzz(countTo) {
let arr = [];
for (let i = 1; i <= countTo; i ++) {
 /*
Instead of checking the values of i % 3 === 0
or i % 5 === 0 each time, create a variable for both, and reuse that variable for each check
*/
  if (i % 3 === 0 && i % 5 === 0) {
    arr.push("fizzbuzz")
  } else if (i % 3 === 0) {
    arr.push("fizz")
  } else if (i % 5 === 0) {
    arr.push("buzz")
  } else {
    arr.push(i);
  }
}
return arr;
}

In this case, the difference in performance is small, but real.

More importantly, it's best practice keep your code DRY (Do Not Repeat). DRY code can lead to quicker knowledge-share on a team, and fewer bugs because the values are traceable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment