-
Notifications
You must be signed in to change notification settings - Fork 0
/
letterPositions.js
53 lines (43 loc) · 1.59 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
//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.
//eqArrays function
const eqArrays = function(Array1, Array2) {
if (Array.isArray(Array1) &&
Array.isArray(Array2)) {
for (const [i, value] of Array1.entries()) {
const value2 = Array2[i];
if (value !== value2) {
return false;
}
}
} return true;
};
//assertArraysEqual function
const assertArraysEqual = function(Array1, Array2) {
if (eqArrays(Array1, Array2)) {
console.log(`Assertion Passed: ${Array1} ✅✅✅ ${Array2}`);
} else {
console.log(`Assertion Failed: ${Array1} 🛑🛑🛑 ${Array2}`);
}
};
// We'll implement a new function letterPositions which will return all the indices (zero-based positions) in the string where each character is found.
//letterPositions function
const letterPositions = function(sentence) {
//variable
const results = {};
//for loop to go through each occurrence
for (let i = 0; i < sentence.length; i++) {
if(results[sentence[i]]) {
results[sentence[i]].push(i);
} else {
results[sentence[i]] = [i];
}
}
// logic to update results here
delete results[" "];
return results;
}
module.exports = letterPositions;
//Test code
assertArraysEqual(letterPositions("hello").e, [1]);
console.log(letterPositions("I have the cutest bunny"));