-
Notifications
You must be signed in to change notification settings - Fork 0
/
letterPosition.js
51 lines (47 loc) · 1.17 KB
/
letterPosition.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
//ASSERTION
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`✅ Assertion Passed: ${actual} === ${expected}` );
} else {
console.assert(false, `🔴 ${actual} !== ${expected}`);
}
};
//EQ ARRAYS
const eqArrays = function(array1, array2) {
if (array1.length !== array2.length) {
return false;
}
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
};
//ASSERT ARRAY EQUAL
const assertArrayEqual = function(array1, array2) {
if (eqArrays(array1, array2)) {
console.log(`✅ Assertion Passed: ${array1} === ${array2}` );
return;
}
console.assert(false, `🔴 ${array1} !== ${array2}`);
}
// LETTER POSITIONS FUNCTION
const letterPositions = function(string) {
let output = {}
for (let i = 0; i < string.length; i++) {
const letter = string[i];
if (letter === " ") {
} else {
if (output[letter]) {
output[letter].push(i);
}else{
let array = [i]
output[letter] = array;
}
}
}
return output;
}
console.log(letterPositions("hello"))
assertArrayEqual(letterPositions("hello").e, [1]);