-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
184 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Sort an array of strings alphabetically | ||
|
||
// Write a function that takes an array of strings as argument | ||
// It should return the array with its values sorted alphabetically | ||
|
||
function myFunction(arr) { | ||
return arr.sort(); | ||
} | ||
|
||
console.log(myFunction(["b", "c", "d", "a"])); // ['a', 'b', 'c', 'd'] | ||
console.log(myFunction(["z", "c", "d", "a", "y", "a", "w"])); // ['a', 'a', 'c', 'd', 'w', 'y', 'z'] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Sort an array of numbers in descending order | ||
|
||
// Write a function that takes an array of numbers as argument | ||
// It should return an array with the numbers sorted in descending order | ||
|
||
function myFunction(arr) { | ||
return arr.sort((a, b) => b - a); | ||
} | ||
|
||
console.log(myFunction([1, 3, 2])); // [3,2,1] | ||
console.log(myFunction([4, 2, 3, 1])); // [4,3,2,1] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Return last n array elements | ||
|
||
// Write a function that takes an array and a number (n) as arguments | ||
// It should return the last n array elements | ||
// If the array has less than n elements, return all | ||
|
||
function myFunction(arr, n) { | ||
return arr.splice(-n); | ||
|
||
// AUTHOR'S | ||
// return arr.slice(-n); | ||
} | ||
|
||
console.log(myFunction([1, 2, 3, 4, 5], 2)); // [4, 5] | ||
console.log(myFunction([1, 2, 3], 6)); // [1, 2, 3] | ||
console.log(myFunction([1, 2, 3, 4, 5, 6, 7, 8], 3)); // [6, 7, 8] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Sum up all array elements with values greater than | ||
|
||
// Write a function that takes an array and a number, say num, as arguments | ||
// Sum up all array elements with a value greater than num | ||
// Return the sum | ||
|
||
function myFunction(arr, num) { | ||
return arr.filter((el) => el > num).reduce((acc, cur) => acc + cur); | ||
|
||
// AUTHOR'S: | ||
// return arr.reduce((sum, cur) => { | ||
// if (cur > num) return sum + cur; | ||
// return sum; | ||
// }, 0); | ||
} | ||
|
||
console.log(myFunction([1, 2, 3, 4, 5, 6, 7], 2)); // 25 | ||
console.log(myFunction([-10, -11, -3, 1, -4], -3)); // 1 | ||
console.log(myFunction([78, 99, 100, 101, 401], 99)); // 602 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Return the average of an array of numbers | ||
|
||
// Write a function that takes an array of numbers as argument | ||
// It should return the average of the numbers | ||
|
||
function myFunction(arr) { | ||
return arr.reduce((acc, cur) => acc + cur, 0) / arr.length; | ||
} | ||
|
||
console.log(myFunction([10, 100, 40])); // 50 | ||
console.log(myFunction([10, 100, 1000])); // 370 | ||
console.log(myFunction([-50, 0, 50, 200])); // 50 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Check if all array elements are equal | ||
|
||
// Write a function that takes an array as argument | ||
// It should return true if all elements in the array are equal | ||
// It should return false otherwise | ||
|
||
function myFunction(arr) { | ||
return arr.every((el) => el === arr[0]); | ||
|
||
// AUTHOR'S: | ||
// return new Set(arr).size === 1; | ||
} | ||
|
||
console.log(myFunction([true, true, true, true])); // true | ||
console.log(myFunction(["test", "test", "test"])); // true | ||
console.log(myFunction([1, 1, 1, 2])); // false | ||
console.log(myFunction(["10", 10, 10, 10])); // false |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Remove a specific array element | ||
|
||
// Write a function that takes an array and a value as argument | ||
// The function should clean the array from all occurrences of the given value and return the the cleaned version | ||
|
||
function myFunction(arr, val) { | ||
return arr.filter((el) => el !== val); | ||
} | ||
|
||
console.log(myFunction([1, 2, 3], 2)); // [1, 3] | ||
console.log(myFunction([1, 2, "2"], "2")); // [1, 2] | ||
console.log(myFunction([false, "2", 1], false)); // ['2', 1] | ||
console.log(myFunction([1, 2, "2", 1], 1)); // [2, '2'] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Create a range of numbers | ||
|
||
// Write a function that takes two numbers, say min and max, as arguments | ||
// Return an array of numbers in the range min to max | ||
|
||
function myFunction(min, max) { | ||
const arr = []; | ||
for (let i = min; i <= max; i++) arr.push(i); | ||
return arr; | ||
} | ||
|
||
console.log(myFunction(2, 10)); // [2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
console.log(myFunction(1, 3)); // [1, 2, 3] | ||
console.log(myFunction(-5, 5)); // [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] | ||
console.log(myFunction(2, 7)); // [2, 3, 4, 5, 6, 7] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Return the longest string from an array of strings | ||
|
||
// Write a function that takes an array of strings as argument | ||
// It should return the longest string | ||
|
||
// function myFunction(arr) { | ||
// return arr.reduce((acc, cur) => { | ||
// if (cur.length > acc.length) return (acc = cur); | ||
// return acc; | ||
// }, ""); | ||
// } | ||
|
||
// function myFunction(arr) { | ||
// let acc = ""; | ||
|
||
// arr.forEach((el) => { | ||
// if (el.length > acc.length) acc = el; | ||
// }); | ||
|
||
// return acc; | ||
// } | ||
|
||
// AUTHOR'S: | ||
function myFunction(arr) { | ||
return arr.reduce((acc, cur) => (acc.length <= cur.length ? cur : acc)); | ||
} | ||
|
||
console.log(myFunction(["help", "me"])); // 'help' | ||
console.log(myFunction(["I", "need", "candy"])); // 'candy' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Merge an arbitrary number of arrays | ||
|
||
// Write a function that takes arguments an arbitrary number of arrays | ||
// It should return an array containing the values of all arrays | ||
|
||
function myFunction(...arr) { | ||
return arr.flat(); | ||
} | ||
|
||
console.log(myFunction([1, 2, 3], [4, 5, 6])); // [1, 2, 3, 4, 5, 6] | ||
console.log(myFunction(["a", "b", "c"], [4, 5, 6])); // ['a', 'b', 'c', 4, 5, 6] | ||
console.log(myFunction([true, true], [1, 2], ["a", "b"])); // [true, true, 1, 2, 'a', 'b'] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function myFunction(a, b) { | ||
// return Array.from(new Set([...a, ...b])).sort((a, b) => a - b); | ||
|
||
// const arr = []; | ||
// a.concat(b).forEach((el) => { | ||
// if (!arr.includes(el)) arr.push(el); | ||
// }); | ||
// return arr.sort((a, b) => a - b); | ||
|
||
// AUTHOR'S: | ||
return [...new Set([...a, ...b])].sort((a, b) => a - b); | ||
} | ||
|
||
console.log(myFunction([1, 2, 3], [3, 4, 5])); // [1, 2, 3, 4, 5] | ||
console.log(myFunction([-10, 22, 333, 42], [-11, 5, 22, 41, 42])); // [-11, -10, 5, 22, 41, 42, 333] |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.