-
Notifications
You must be signed in to change notification settings - Fork 0
/
letterPositions.js
59 lines (46 loc) · 1.79 KB
/
letterPositions.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const assertArraysEqual = function(array1, array2) {
if (eqArrays(array1, array2)) {
console.log(`✅✅✅ Assertion Passed: Arrays equal - ${array1} === ${array2}`);
} else {
console.log(`⛔⛔⛔ Assertion Failed: Arrays not equal - ${array1} !== ${array2}`);
}
};
const eqArrays = function(array1, array2) {
//check if both arrays have the same length
if (array1.length !== array2.length) {
//checks if both arrays have the same elements
return false;
}
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
};
//We'll implement a new function letterPositions which will return all the indices (zero-based positions) in the string where each character is found.
// For each letter, instead of returning just one number to represent its number of occurrences, multiple numbers may be needed to represent all the places in the string that it shows up.
const letterPositions = function(sentenceString) {
// resulting object provides an array with indices for each letter
const position = {};
for (let i = 0; i < sentenceString.length; i++) {
//to avoid blank spaces within the sentence
if (sentenceString[i] !== " ") {
//if the letter already exists, push the index into the same array
if (position[sentenceString[i]]) {
position[sentenceString[i]].push(i);
//otherwise create a new array for that letter
} else {
position[sentenceString[i]] = [i];
}
}
}
return position;
};
console.log(letterPositions("hello"));
const result = letterPositions("hello");
//result.letter to access the letter from the object
assertArraysEqual(result.h, [0]);
assertArraysEqual(result.e, [1]);
assertArraysEqual(result.l, [2, 3]);
assertArraysEqual(result.o, [4]);