1. Write a function that takes an array (a)
and a value (n)
as argument Return the nth
element of 'a'
function myFunction(a, n) {
//code
}
myFunction([1, 2, 3, 4, 5], 3);
Solution
function myFunction(a, n) {
return a[n - 1];
}
console.log(myFunction([1, 2, 3, 4, 5], 3));
2. Write a function that takes two values, say a
and b
, as arguments. Return true
if the two values are equal and of the same type
function myFunction(a, b) {
//code
}
myFunction(2, 3);
Solution
function myFunction(a, b) {
return a === b;
}
console.log(myFunction(2, 3));
function myFunction(a) {
//code
}
myFunction([2, 3, 4, 5]);
Solution
function myFunction(a) {
return a.length;
}
console.log(myFunction([1, 2, 2, 4]));
4. Write a function that a string (a)
as argument Create an object
that has a property with key 'key'
and a value of (a)
Return the object
function myFunction(a) {
//code
}
myFunction("z");
Solution
function myFunction(a) {
return { key: a };
}
console.log(myFunction("z"));
5. Write a function that takes a string
as argument. Extract the last 3
characters from the string. Return the result
function myFunction(str) {
//code
}
myFunction("string");
Solution
function myFunction(str) {
return str.slice(-3);
}
console.log(myFunction("string"));
6. Write a function that takes a string
(a)
as argument. Extract the first half
a. Return the result
function myFunction(a) {
//code
}
myFunction("string");
Solution
function myFunction(a) {
return a.slice(0, a.length / 2);
}
console.log(myFunction("string"));
7. Write a function that takes a string (a)
as argument, Get the first 3
characters of a Return the result
function myFunction(a) {
//code
}
myFunction("string");
Solution
function myFunction(a) {
return a.slice(0, 3);
}
console.log(myFunction("string"));
8. Write a function that takes an array (a)
as argument, Extract the first 3
elements of a Return the resulting array
function myFunction(a) {
//code
}
myFunction([1, 2, 3, 4, 5]);
Solution
function myFunction(a) {
return a.slice(0, 3);
}
console.log(myFunction([1, 2, 3, 4, 5]));
function myFunction(a) {
//code
}
myFunction([1, 2, 3, 4, 5]);
Solution
function myFunction(a) {
return typeof a;
}
console.log(myFunction([1, 2, 3, 4, 5]));
10. Write a function that takes an array
of strings
as argument, Sort
the array elements
alphabetically
, Return the result
function myFunction(arr) {
//code
}
myFunction(["hello", "world", "learning", "js", "is", "fun"]);
Solution
function myFunction(arr) {
return arr.sort();
}
console.log(myFunction(["b", "c", "f", "e", "d", "a"]));
11. Write a function that takes a string (a)
and a number (n)
as argument. Return the nth
character of 'a'
function myFunction(a, n) {
//code
}
myFunction("javascript", 5);
Solution
function myFunction(a, n) {
return a.charAt(n - 1); //also a[n-1]
}
console.log(myFunction("javascript", 5));
12. Write a function that takes an array (a)
as argument, Remove the first 3
elements of 'a'
, Return the result
function myFunction(a) {
//code
}
myFunction([1, 2, 3, 4, 5, 6]);
Solution
function myFunction(a) {
return a.slice(3);
}
console.log(myFunction([1, 2, 3, 4, 5, 6]));
13. Write a function that takes an array (a)
as argument, Extract the last 3
elements of a
, Return the resulting array
function myFunction(a) {
//code
}
myFunction([1, 2, 3, 4, 5, 6]);
Solution
function myFunction(a) {
return a.slice(-3);
}
console.log(myFunction([1, 2, 3, 4, 5, 6]));
14. Write a function that takes a string (a)
as argument, Remove the last 3 characters
of a
, Return the result
function myFunction(a) {
//code
}
myFunction("javascript");
Solution
function myFunction(a) {
return a.slice(0, -3);
}
console.log(myFunction("javascript"));
function myFunction(a, b) {
//code
}
myFunction(1, 2);
Solution
function myFunction(a, b) {
return a + b;
}
console.log(myFunction(1, 2));
function myFunction(a, b) {
//code
}
myFunction(20, 40);
Solution
function myFunction(a, b) {
return (b / 100) * a;
}
console.log(myFunction(20, 40));
17. Write a function that takes a string (a)
as argument, Remove the first 3
characters of a
, Return the result
function myFunction(a) {
//code
}
myFunction("hello");
Solution
function myFunction(a) {
return a.slice(3);
}
console.log(myFunction("hello world"));
18. Write a function that takes a Set
and a value
as arguments, Check if the value
is present in the Set
function myFunction(set, val) {
//code
}
myFunction(new Set([1, 2, 3]), 2);
Solution
function myFunction(set, val) {
return set.has(val);
}
console.log(myFunction(new Set([1, 2, 3]), 2));
19. Write a function that takes two strings (a and b)
as arguments, Create an object
that has a property with key 'a'
and a value of 'b'
, Return the object
function myFunction(a, b) {
//code
}
myFunction(("name", "pavan"));
Solution
function myFunction(a, b) {
return { [a]: b };
}
console.log(myFunction("name", "pavan"));
20. Write a function that takes 6
values (a,b,c,d,e,f)
as arguments, Sum
a and b, Then substract
by c, Then multiply
by d and divide
by e, Finally raise to the power of f
and return the result, Tipp: mind the order
function myFunction(a, b, c, d, e, f) {
//code
}
myFunction((6, 2, 1, 4, 2, 3));
Solution
function myFunction(a, b, c, d, e, f) {
return (((a + b - c) * d) / e) ** f;
}
console.log(myFunction(6, 2, 1, 4, 2, 3));