-
Parameters and return values
Given this function:
function add(a, b) { return a + b; }
1.1.1 How many parameters
add
accepts?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
add
accepts?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 }
sum
is 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
numbers
insum
's body?1.3.2 Implement
sum
with the above function signature. -
Arrow functions
Given this function:
function isNeg(n) { return n < 0; }
2.1.1 How many ways can we rewrite
isNeg
as an arrow function? (assuming we keep the expressionn < 0
unchanged)2.1.2 What is the least verbose way (least characters used) to rewrite
isNeg
as an arrow function?Now, if we have this function:
function assignLegalAge(obj) { obj.age = 18; }
assignLegalAge
takes in an objectobj
and writes propertyobj.age
to 18.2.1.3 How many ways can we rewrite
assignLegalAge
as 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
assignLegalAge
as an arrow function?2.1.5 What is the difference between
assignLegalAge
andisNeg
that 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
string
type2.3.3 Give 3 examples of methods on JavaScript
Array
type2.4.1 What is the return value of a callback function passed into
Array.filter
method?2.4.2 What is the return type of the
Array.filter
method?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.map
andnumber.filter
is identical: a named functioncb
.Replace
cb
with anonymous function that would make variablesisNegs
become[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
cb
as an annonymous arrow function such thatresult
is[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