Is it there? includes
Arrays come with ready-made operations that save us writing loops: the array name, a dot, then the operation. For example, list.includes(value) returns true if the value is in the array, and false if it is not:
const wordList = ["dog", "dog", "dog", "dag", "dog"];
console.log(wordList.includes("dag"));
console.log(wordList.includes("cat"));true false
Where is it? indexOf
list.indexOf(value) returns the index of the value (from 0); if it appears more than once you get the first position. If it is not there you get -1:
const wordList = ["bat", "bat", "bat", "bat", "bat", "bat", "bit", "bat"];
console.log(wordList.indexOf("bit"));
console.log(wordList.indexOf("pen"));6 -1
indexOf with a capital O, or you get an error.Sorting with sort, flipping with reverse
list.sort() puts strings in alphabetical order, A to Z, and list.reverse() flips the order so the last becomes first. Both change the array itself, so print it on a line after:
const nameList = ["Emma", "Sophia", "Oliver", "Alex"]; nameList.sort(); console.log(nameList); nameList.reverse(); console.log(nameList);
["Alex", "Emma", "Oliver", "Sophia"] ["Sophia", "Oliver", "Emma", "Alex"]
sort() with nothing in the brackets treats every value as text, even numbers! It compares the first digit first, so 100 comes before 25 and 9:const nums = [25, 100, 9, 41]; nums.sort(); console.log(nums);
[100, 25, 41, 9]
To sort numbers, give sort a compare function that takes two values a and b and returns a - b: a negative result puts a first. For largest to smallest, return b - a:
function smallFirst(a, b) {
return a - b;
}
function bigFirst(a, b) {
return b - a;
}
const nums = [25, 100, 9, 41];
nums.sort(smallFirst);
console.log(nums);
nums.sort(bigFirst);
console.log(nums);[9, 25, 41, 100] [100, 41, 25, 9]
Practice
Print whether the word list contains "bell". (Output: true)
const wordList = ["ball", "ball", "ball", "ball", "bell", "ball", "ball"]; console.log(wordList.includes(___));
Press Run to see the result. On line 3, print the index of the word "pen" in the word list.
const wordList = ["pan", "pan", "pen", "pan", "pan", "pan", "pan", "pan"];
Press Run to see the result. Complete line 3 to sort guestList alphabetically before it is printed.
let guestList = ["Johnson", "Smith", "Brown", "Davis", "Wilson"]; ___ console.log(guestList);
Press Run to see the result. From line 3: reverse the order of rankingList, then print it.
const rankingList = ["Mason", "Isabella", "William", "Charlotte", "Mohamed"];
Press Run to see the result. Sort the marks from smallest to largest as numbers and print them. Output: [9, 23, 45, 78, 100]
Hint
marks.sort() alone gives [100, 23, 45, 78, 9]. You need a compare function that returns a - b.const marks = [45, 100, 9, 78, 23];
Press Run to see the result. Competition scores. Sort them from largest to smallest and print the array, then print the winning score (the first value). Output: [100, 95, 91, 88, 72] then Winner: 100.
const scores = [88, 95, 72, 100, 91];
Press Run to see the result. The canteen queue. Print Omar's place in the queue, counting from 1: Omar is number 3. Use indexOf.
Hint
indexOf starts at 0, so add 1. Put the addition in brackets so it is not glued on as text.const queue = ["Ali", "Mona", "Omar", "Sara"];
Press Run to see the result. Key points
list.includes(v)→ true or false;list.indexOf(v)→ the index, or-1.list.sort()sorts alphabetically,list.reverse()flips the order; both change the array itself.- To sort numbers:
list.sort(f)wheref(a, b)returnsa - b(orb - afor largest first).