-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallengs-22.js
36 lines (26 loc) · 1.13 KB
/
Challengs-22.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Write a function that takes an array of strings as an argument and returns a sorted array containing the same strings, ordered from shortest to longest.
// For example, if this array were passed as an argument:
// ["Telescopes", "Glasses", "Eyes", "Monocles"]
// Your function would return the following array:
// ["Eyes", "Glasses", "Monocles", "Telescopes"]
// All of the strings in the array passed to your function will be different lengths, so you will not have to decide how to order multiple strings of the same length.
// #solution
function sortByLength(array) {
// Return an array containing the same strings, ordered from shortest to longest
return array.sort((a, b) => a.length - b.length);
}
sortByLength(["Beg", "Life", "I", "To"]);
// ---------------------------- or ------------------------------
function test(arr) {
for (let i = 0; i < arr.length; i++) {
for (j = 0; j < arr.length - i - 1; j++) {
if (arr[j].length > arr[j + 1].length) {
let result = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = result;
}
}
}
return arr;
}
test(["Telescopes", "Glasses", "Eyes", "Monocles"]);