|
| 1 | +/* |
| 2 | +At the start of the course, you worked in teams to sort your team members, labelled by |
| 3 | +numbers, in ascending or descending order. |
| 4 | +
|
| 5 | +Today, you will be applying the sorting algorithm you used in that exercise in code! |
| 6 | +
|
| 7 | +Create a function called sortAges which: |
| 8 | +- takes an array of mixed data types as input |
| 9 | +- removes any non-number data types without using the built-in javascript filter method |
| 10 | +- returns an array of sorted ages in ascending order |
| 11 | + - HARD MODE - without using the built-in javascript sort method 😎 |
| 12 | +
|
| 13 | +You don't have to worry about making this algorithm work fast! The idea is to get you to |
| 14 | +"think" like a computer and practice your knowledge of basic JavaScript. |
| 15 | +*/ |
| 16 | + |
| 17 | +function sortAges(arr) {} |
| 18 | + |
| 19 | +/* ======= TESTS - DO NOT MODIFY ===== */ |
| 20 | + |
| 21 | +const agesCase1 = [ |
| 22 | + "🎹", |
| 23 | + 100, |
| 24 | + "💩", |
| 25 | + 55, |
| 26 | + "🥵", |
| 27 | + "🙈", |
| 28 | + 45, |
| 29 | + "🍕", |
| 30 | + "Sanyia", |
| 31 | + 66, |
| 32 | + "James", |
| 33 | + 23, |
| 34 | + "🎖", |
| 35 | + "Ismeal", |
| 36 | +]; |
| 37 | +const agesCase2 = ["28", 100, 60, 55, "75", "🍕", "Elamin"]; |
| 38 | + |
| 39 | +function arraysEqual(a, b) { |
| 40 | + if (a === b) return true; |
| 41 | + if (a == null || b == null) return false; |
| 42 | + if (a.length != b.length) return false; |
| 43 | + |
| 44 | + for (let i = 0; i < a.length; ++i) { |
| 45 | + if (a[i] !== b[i]) return false; |
| 46 | + } |
| 47 | + |
| 48 | + return true; |
| 49 | +} |
| 50 | + |
| 51 | +function test(test_name, expr) { |
| 52 | + let status; |
| 53 | + if (expr) { |
| 54 | + status = "PASSED"; |
| 55 | + } else { |
| 56 | + status = "FAILED"; |
| 57 | + } |
| 58 | + |
| 59 | + console.log(`${test_name}: ${status}`); |
| 60 | +} |
| 61 | + |
| 62 | +test( |
| 63 | + "sortAges function works - case 1", |
| 64 | + arraysEqual(sortAges(agesCase1), [23, 45, 55, 66, 100]) |
| 65 | +); |
| 66 | + |
| 67 | +test( |
| 68 | + "sortAges function works - case 2", |
| 69 | + arraysEqual(sortAges(agesCase2), [55, 60, 100]) |
| 70 | +); |
0 commit comments