-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletterPositions.js
More file actions
38 lines (32 loc) · 884 Bytes
/
letterPositions.js
File metadata and controls
38 lines (32 loc) · 884 Bytes
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
const letterPositions = function(sentence) {
const results = {};
let string = sentence.split(' ').join('');
for(let i = 0; i < string.length; i++) {
let char = string[i];
if(results[char]){
results[char].push(i);
} else {
results[char] = [i];
}
}
return results;
};
const assertArraysEqual = function (array1, array2) {
for (let i = 0; i < array1.length; i++ ) {
if (array1[i] !== array2[i]) {
console.log(`The '${array1}' and the '${array2}' are not same ❌`);
return false;
}
}
console.log(`The '${array1}' and the '${array2}' are the same✅`);
};
const eqArrays = function (array1, array2) {
for (let i = 0; i < array1.length; i++ ) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
};
assertArraysEqual(letterPositions('hello').l, [2, 3]);
console.log(letterPositions('hello'))