-
Write a function called
averagethat takes an array of numbers as a parameter and returns the average of those numbers.After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
-
Write a function called
minthat finds the smallest number in an array of numbers.After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
-
Write a function
shortestWordthat works likelongestWord, but returns the shortest word instead.After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
-
Write a function
countCharactersthat, when given a string as an argument, returns an object containing counts of the ocurrences of each character in the string.function countCharacters(s) { // ... } countCharacters("hello"); // => {"h": 1, "e": 1, "l": 2, "o": 1}
After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
HINT: You will want to make use of the string method
split. Try\"hello".split("")at a console to see how it works. -
Write a function
selectthat accepts two arguments: an object and an array. The array should contain names of keys that will be selected from the object:function select(obj, keys) { // ... } select({a: 1, b: 2, c: 3}, ["a"]); // => {a: 1} select({a: 1, b: 2, c: 3}, ["a", "c"]); // => {a: 1, c: 3} select({a: 1, b: 2, c: 3}, ["a", "c", "d"]); // => {a: 1, c: 3}
After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
-
Complete the below function called
rangethat takes two integers as parameters,startandend, and returns an array containing all the whole numbers between them starting withstartand up toend(you can use aforloop,whileloop,each, or repetition with function invocation). The function definition should look like this:function range(start, end) { // YOUR CODE HERE }
You should be able to use it like so:
range(0, 4); // => [0, 1, 2, 3] range(2, 7); // => [2, 3, 4, 5, 6] range(10, 10); // => [] range(10, 2); // => []
After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
-
Write a function called
squareNumericValuesthat takes an object as a parameter and returns an object with all of the numeric values in the object squared, e.g.function squareNumericValues(obj) { // TODO: Your code here } squareNumericValues({a: 4, b: 7, c: 2}); // => {a: 16, b: 49, c: 4} squareNumericValues({name: "Phuong", age: 25}); // => {name: "Phuong", age: 625}
One observation to make when writing this function is that you'll need to only square the values that are actually numbers -- notice how in the second example invocation above (
squareNumericValues({name: "Phuong", age: 25})) the value "Phuong" is unchanged because its value is a string.To handle this, you will need to use the
typeofoperator to determine each value's type. Enter the following into a console to get an idea of howtypeofworks:typeof 1; // => "number" typeof "hello"; // => "string" typeof true; // => "boolean"
After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
-
Complete the below function called
rangethat takes two integers as parameters,startandend, and returns an array containing all the whole numbers between them starting withstartand up toend(you can use aforloop,whileloop,each, or repetition with function invocation). The function definition should look like this:function range(start, end) { // YOUR CODE HERE }
You should be able to use it like so:
range(0, 4); // => [0, 1, 2, 3] range(2, 7); // => [2, 3, 4, 5, 6] range(10, 10); // => [] range(10, 2); // => []
After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
-
Given the following array of
people, write a function that, when passedpeopleas a parameter, returns the person (that is, your function should return an object) with the longest name (first, middle & last).var people = [ {name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26}, {name: {first: "Ben", last: "Bitdiddle"}, age: 34}, {name: {first: "Eva", middle: "Lu", last: "Ator"}, age: 40}, {name: {first: "Lem", middle: "E.", last: "Tweakit"}, age: 45}, {name: {first: "Louis", last: "Reasoner"}, age: 21} ]; function longestName(people) { // TODO: Your code here } longestName(people); // => {name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26}
After you write your function, you can test it using the above inputs to make sure that it behaves correctly.
HINT: It might be helpful to have a fullName function that, when given
a person as a parameter, returns a person's full name.