Skip to content

Instantly share code, notes, and snippets.

@CodeSigils
Last active April 3, 2018 08:29
Show Gist options
  • Save CodeSigils/471a2702a5eb6f93d3953911a147554a to your computer and use it in GitHub Desktop.
Save CodeSigils/471a2702a5eb6f93d3953911a147554a to your computer and use it in GitHub Desktop.
Arrays and Loops Drills
/* ======= max and min (without sort) ======= */
/*
To complete this drill, you need to implement two functions, max and min.
Both functions should take a single argument: a list of numbers called numbers.
max(myNumbers) should return the largest number in the list, while min(myNumbers)
should return the smallest.
Assume that the numbers input only contains numbers (that is, you don't have to
inspect your inputs to confirm they only contain numbers).
If the numbers array is empty, then both max and min should return null or
undefined, whichever is more convenient.
Finally, note that you are not allowed to use JavaScript's sort method which
provides a simple-but-inefficient way to solve this problem (simply sort the
list and return either the first or last element, depending on if you need min
or max).
*/
function max(numbers) {
var maxNum = numbers[0];
for (var i=0; i <= numbers.length; i++) {
if (numbers[i] > maxNum) {
maxNum = numbers[i];
}
}
return maxNum;
}
console.log(max([1, 2, 3, 4, 5, 6, 7]));
function min(numbers) {
var minNum = numbers[0];
for (var i=0; i <= numbers.length; i++) {
if (numbers[i] < minNum) {
minNum = numbers[i];
}
}
return minNum;
}
console.log(min([1, 2, 3, 4, 5, 6, 7]));
/* ======= Compute the average ======= //
Write a function called average that takes a list of numbers as its input,
and returns the average generated from the list of numbers.
Recall that to compute the average of a set of numbers, you add together all
of the numbers, and then divide the resulting value by the number of items
in the list.
*/
function average(numbers) {
var sum = numbers[0];
for (var i = 1; i < numbers.length; i++) {
sum = sum + numbers[i];
}
return sum / numbers.length;
}
console.log(average([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]));
/* ======= FizzBuzz ======= //
Write a function that implements the FizzBuzz game. FizzBuzz is a game used to
teach multiples/divisors. The player is given a number to count to.
Then, they count from one up to that number.
For numbers divisible by 3, they substitute the word "fizz",
for numbers divisible by 5, they subsitute the word "buzz",
and for numbers divisible by both 3 and 5, they substitute the word "fizzbuzz".
Your fizzbuzz function should return an array of FizzBuzz values.
It should take a single parameter, countTo, which determines how long the
returned array of values is.
*/
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;
}
console.log(fizzBuzz(15));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment