Skip to content

Commit

Permalink
arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kvdos committed Sep 12, 2021
1 parent b0ffb5a commit f2a257b
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 13 deletions.
Binary file added .DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions 03 - Javascript arrays/01.js
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']
11 changes: 11 additions & 0 deletions 03 - Javascript arrays/02.js
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]
16 changes: 16 additions & 0 deletions 03 - Javascript arrays/03.js
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]
19 changes: 19 additions & 0 deletions 03 - Javascript arrays/04.js
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
12 changes: 12 additions & 0 deletions 03 - Javascript arrays/05.js
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
17 changes: 17 additions & 0 deletions 03 - Javascript arrays/06.js
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
13 changes: 13 additions & 0 deletions 03 - Javascript arrays/07.js
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']
15 changes: 15 additions & 0 deletions 03 - Javascript arrays/08.js
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]
29 changes: 29 additions & 0 deletions 03 - Javascript arrays/09.js
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'
12 changes: 12 additions & 0 deletions 03 - Javascript arrays/10.js
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']
15 changes: 15 additions & 0 deletions 03 - Javascript arrays/11.js
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]
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# JSchallenger

Learn Javascript online by solving coding exercises.
[JSchallenger](https://www.jschallenger.com)
[JSchallenger](https://www.jschallenger.com) Learn Javascript online by solving coding exercises.

![alt text](README/logo.png)

- What: JavaScript challenges from JSchallenger.com. It includes my own code in case I got it right as well as challenge's author code.
- Why: Just in case someone is starting, as I am currently, to learn JS and wants to practice.
Expand Down Expand Up @@ -30,17 +31,17 @@ Big thanks to the author of JSchallenger.com. It's not easy to find such good ex

## Javascript arrays

- [Sort an array of strings alphabetically]()
- [Sort an array of numbers in descending order]()
- [Return last n array elements]()
- [Return the average of an array of numbers]()
- [Sum up all array elements with values greater than]()
- [Check if all array elements are equal]()
- [Remove a specific array element]()
- [Create a range of numbers]()
- [Return the longest string from an array of strings]()
- [Merge an arbitrary number of arrays]()
- [Merge two arrays with duplicate values]()
- [Sort an array of strings alphabetically](03%20-%20Javascript%20arrays/01.js)
- [Sort an array of numbers in descending order](03%20-%20Javascript%20arrays/02.js)
- [Return last n array elements](03%20-%20Javascript%20arrays/03.js)
- [Return the average of an array of numbers](03%20-%20Javascript%20arrays/04.js)
- [Sum up all array elements with values greater than](03%20-%20Javascript%20arrays/05.js)
- [Check if all array elements are equal](03%20-%20Javascript%20arrays/06.js)
- [Remove a specific array element](03%20-%20Javascript%20arrays/07.js)
- [Create a range of numbers](03%20-%20Javascript%20arrays/08.js)
- [Return the longest string from an array of strings](03%20-%20Javascript%20arrays/09.js)
- [Merge an arbitrary number of arrays](03%20-%20Javascript%20arrays/10.js)
- [Merge two arrays with duplicate values](03%20-%20Javascript%20arrays/11.js)

## Javascript dates

Expand Down
Binary file added README/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f2a257b

Please sign in to comment.