This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Modify personMaker.fullName to be a function that uses self-reference (via this) in order to return the firstName and lastName properties separated by a space. | |
| // So, for instance, if firstName was 'Jane' and lastName was 'Doe', fullName() should return 'Jane Doe'. | |
| function personMaker() { | |
| var person = { | |
| firstName: 'Paul', | |
| lastName: 'Jones', | |
| fullName: function() { | |
| return this.firstName + ' ' + this.lastName; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Modify the keyDeleter function so that it deletes keys for foo and bar for any object passed in and then returns the modified object. | |
| const sampleObj = { | |
| foo: 'foo', | |
| bar: 'bar', | |
| bizz: 'bizz', | |
| bang: 'bang', | |
| }; | |
| function keyDeleter(obj) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 substitute the word "buzz", and for numbers divisible by both 3 and 5, they substitute the word "fizzbuzz". | |
| // In this drill, you'll write a function called fizzBuzz that generates the Fizzbuzz sequence up to a certain number. | |
| // 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. | |
| // So, for instance, if it gets called like this: fizzBuzz(15), it should return: | |
| function fizzBuzz(countTo) { | |
| let a = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Write a function called average that takes an array of numbers as its input and returns the average generated from the array 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 array. | |
| function average(numbers) { | |
| let sum =0; | |
| for (let i=0; i < numbers.length; i++) { | |
| //for (let i=1; i < numbers.length + 1; i++) | |
| //for (let i=1; i <= numbers.length; i++) | |
| sum = sum + numbers[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // To complete this drill, you need to implement 2 functions, max and min. Both functions should take a single argument: an array of numbers called numbers. max(numbers) should return the largest number in the array, while min(numbers) 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 array and return either the first or last element, depending on if you need min or max). Also, for obvious reasons, you are not allowed to use Math.max or Math.min. | |
| function max(numbers) { | |
| let max = numbers[0]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Write a function called divisibleBy5 that takes a single argument: an array of numbers called array. The function should return the first value in array that is divisible by 5. If no values are divisible by five, it should return undefined. To complete this drill you'll need to use the array .find method described in the previous assignment. | |
| function divisibleBy5(array) { | |
| //return array.find(example); | |
| return array.find(element => element % 5 === 0); | |
| } | |
| // function example(element) { | |
| // return element % 5 === 0; | |
| // } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Write a function called shortWords that takes a single argument: an array of strings called array. The function should return an array with only strings that are shorter than 5 characters. To complete this drill you'll need to use the array .filter method described in the previous assignment. | |
| function shortWords(array) { | |
| // return array.filter(check); | |
| return array.filter(element => element.length < 5); | |
| } | |
| function check(element) { | |
| // if (element.length < 5){ | |
| // return true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Write a function called greatestToLeast that takes a single argument: an array of numbers called array. The function should return an array with the numbers sorted from greatest to least. To complete this drill you'll need to use the array .sort method described in the previous assignment. | |
| function greatestToLeast(array) { | |
| return array.sort((a,b) => b-a); | |
| } | |
| /* From here down, you are not expected to | |
| understand.... for now :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Write a function called squares that takes a single argument: an array of values called array. The function should return a new array with the square of each value from the original array. So if the input array was [1, 2, 9, 10], the output should be [1, 4, 81, 100]. You should not use a loop, which will be covered in the next reading. You should use the .map method, which we discussed in the previous assignment. | |
| function squares(array) { | |
| return array.map(par); | |
| } | |
| function par(num1){ | |
| return num1**2; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // To complete this drill, you need to write 2 functions, minusLastItem and copyFirstHalf. Both functions should take a single argument: an array of values called array. | |
| // minusLastItem should return a new array that copies all but the last item in array. copyFirstHalf should return a new array that copies the first half of array. If array has an odd number of values, copyFirstHalf should return the smaller half. For example, if array has 7 values, the first 3 should be copied. | |
| function minusLastItem(array) { | |
| //return array.slice(0,-1); | |
| return array.slice(0, array.length-1); | |
| } | |
| function copyFirstHalf(array) { |