Assessment
/*
CS Prep Assessment
Challenge 1: Select
Define a function select which takes two arguments, an array and a callback function. The callback function can be considered a test in that it will return either true or false depending on the input. select will iterate/loop through the array and pass each array element to the callback as an argument.
If the callback called on a certain value returns true, the original value (the input to the callback) is pushed to a new array. select will return this new array.
Challenge 2: Russian Roulette
Define a function russianRoulette that accepts a number (let us call it n), and returns a function. The returned function will take no arguments, and will return the string ‘click’ the first n - 1 number of times it is invoked. On the very next invocation (the nth invocation), the returned function will return the string ‘bang’. On every invocation after that, the returned function returns the string ‘reload to play again’.
Challenge 3: Golden Sequence
Define a function goldenSequence that takes a number n and returns n's corresponding Fibonacci number.
Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
- Each number is found by adding the previous two
- The first two values, index 0 and index 1, of the sequence shall be 1 and 1
More Info: en.wikipedia.org/wiki/Fibonacci_number
*/
// CHALLENGE 1 - ADD CODE BELOW
/*
Define a function select which takes two arguments, an array and a callback function. The callback function can be considered a test in that it will return either true or false depending on the input. select will iterate/loop through the array and pass each array element to the callback as an argument.
*/
//define a function named select
//select will have two args: an array / callback
function select(myArr, callback1) {
output = [];
//loops through the array: for loop?
for (let i = 0; i < myArr.length; i += 1) {
//passes each index of the array to the callback
//returns true or false: if value is even
// console.log(callback1(myArr[i]));
if (callback1(myArr[i]) === true) {
output.push(myArr[i]);
// console.log(output);
}
}
return output;
}
// // UNCOMMENT THESE LINES TO CHECK YOUR WORK
const arr = [1, 2, 3, 4, 5];
const isEven = n => n % 2 === 0;
console.log(select(arr, isEven)); // should log: [2, 4]
// CHALLENGE 2 - ADD CODE BELOW
/*Challenge 2: Russian Roulette
Define a function russianRoulette that accepts a number (let us call it n), and returns a function. The returned function will take no arguments, and will return the string ‘click’ the first n - 1 number of times it is invoked. On the very next invocation (the nth invocation), the returned function will return the string ‘bang’. On every invocation after that, the returned function returns the string ‘reload to play again’.*/
//make function russianRoulette, takes one argument named 'n'
function russianRoulette(n) {
//returns a function
//returned func takes no arg, returns string 'click'
//put hasBeenrun into closure backpack
let hasBeenRun = 1;
return function() {
//returns click first n - 1 number of times it is invoked
//increment hasBeenRun +1
//loop from zero to n
for (let i = 1; i < n; i += 1) {
if (hasBeenRun < n) {
console.log(hasBeenRun, `hasBeenRun < n`);
hasBeenRun += 1;
return `click`;
//on nth run time, return string 'bang'
} else if (hasBeenRun === n) {
console.log(hasBeenRun, `hasBeenRun === n`);
hasBeenRun += 1;
return `bang`;
} else if (hasBeenRun > n) {
//every subsequent run, return 'reload to play again'
console.log(hasBeenRun, `hasBeenRun > n`);
hasBeenRun += 1;
return `reload to play again`;
}
}
}
}
// UNCOMMENT THESE LINES TO CHECK YOUR WORK
const play = russianRoulette(3);
console.log(play()); // should log: ‘click’
console.log(play()); // should log: ‘click’
console.log(play()); // should log: ‘bang’
console.log(play()); // should log: ‘reload to play again’
console.log(play()); // should log: ‘reload to play again’
// CHALLENGE 3 - ADD CODE BELOW
/*Define a function goldenSequence that takes a number n and returns n's corresponding Fibonacci number.
Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
- Each number is found by adding the previous two
- The first two values, index 0 and index 1, of the sequence shall be 1 and 1
//More Info: en.wikipedia.org/wiki/Fibonacci_number
*/
//make function goldenSequence
//one paramter n
//save the numbers I already did
let savedNumbers = [];
let correspondingFibb = 0;
function goldenSequence(n) {
if (n > 2){
//calculate the Fibb number
let correspondingFibb = goldenSequence(n - 1) + goldenSequence(n - 2);
//at index for given n, save the calculated Fibb number
savedNumbers[n] = correspondingFibb;
// console.log(savedNumbers[n], `savedNumbers[n]`);
// console.log(savedNumbers, `savedNumbers`);
//returns n's corresponding Fibonacci
return correspondingFibb;
//can't figure out how to make it work for num < 2 so these are edge cases
} else if (n === 1 || n === 0) {
return 1;
} else if (n === 2) {
return 2;
//fibbonaci doesn't work with negatives
} else return `it's negative, use a number that's zero or greater`;
}
// UNCOMMENT THESE LINES TO CHECK YOUR WORK
console.log(goldenSequence(0)); // should log: 1
console.log(goldenSequence(1)); // should log: 1
console.log(goldenSequence(2)); // should log: 2
console.log(goldenSequence(3)); // should log: 3
console.log(goldenSequence(4)); // should log: 5
console.log(goldenSequence(5)); // should log: 8
console.log(goldenSequence(12)); // should log: 233
console.log(goldenSequence(-5)); // should log: `it's negative`