-
Parameters and return values
Given this function:
function add(a, b) { return a + b; }
1.1.1 How many parameters
addaccepts?1.1.2 What are the possible return types of
add?1.1.3 What is the return value of
add(1, 2)?1.1.4 What is the return value of
add(1, "2")?1.1.5 What is the return value of
add("foo", "bar", "baz")?1.1.6 What is the return value of
add(1, 2, 3)?1.1.7 What is the return value of
add(10, add(1, 2, 3))?Given this function:
function add(a, b = 10, c = 20) { return a + b + c; }
1.2.1 How many arguments
addaccepts?1.2.2 What is the return value of
add(1, 2)?1.2.3 What is the return value of
add(1)?1.2.4 What is the return value of
add(10, add(1, 2, 3))?function sum(...numbers) { // Body }
sumis supposed to takes in variadic parameters (as indicated by the spread operator...), and returns the sum of all numbers. For example,sum(10, 20, 30)should return60.1.3.1 What is the variable type of
numbersinsum's body?1.3.2 Implement
sumwith the above function signature. -
Arrow functions
Given this function:
function isNeg(n) { return n < 0; }
2.1.1 How many ways can we rewrite
isNegas an arrow function? (assuming we keep the expressionn < 0unchanged)2.1.2 What is the least verbose way (least characters used) to rewrite
isNegas an arrow function?Now, if we have this function:
function assignLegalAge(obj) { obj.age = 18; }
assignLegalAgetakes in an objectobjand writes propertyobj.ageto 18.2.1.3 How many ways can we rewrite
assignLegalAgeas an arrow function? (assuming we keep the statementobj.age = 18;unchanged)2.1.4 What is the least verbose way (least characters used) to rewrite
assignLegalAgeas an arrow function?2.1.5 What is the difference between
assignLegalAgeandisNegthat made your 2.1.3 answer differ from 2.1.1?2.2.1 Rewrite the following snippet with arrow function:
const negs = nums.filter(function (n) { return n < 0; });
2.3.1 What is a method?
2.3.2 Give 3 examples of methods on JavaScript
stringtype2.3.3 Give 3 examples of methods on JavaScript
Arraytype2.4.1 What is the return value of a callback function passed into
Array.filtermethod?2.4.2 What is the return type of the
Array.filtermethod?Now, given this snippet:
const numbers = [-2, -1, 0, 1, 2]; const isNegs = numbers.map(cb); const negs = numbers.filter(cb);
2.4.3 We can see that the callback passed into
number.mapandnumber.filteris identical: a named functioncb.Replace
cbwith anonymous function that would make variablesisNegsbecome[true, true, false, false, false], andnegs[-2, -1].Given this snippet:
const numbers = [10, 20, 30, 40, 50]; const result = numbers.map(cb);
2.5.1 Implement
cbas an annonymous arrow function such thatresultis[1, 3, 5].
Last active
May 31, 2023 13:49
-
-
Save bgbaroo/a7c3eaa06a2df403e6a1e73944b707a9 to your computer and use it in GitHub Desktop.
Cleverse Academy tech test #1: Foundation JavaScript - Functions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment