-
Notifications
You must be signed in to change notification settings - Fork 0
/
letterPositions.js
33 lines (25 loc) · 989 Bytes
/
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
const assertEqual = require('./assertEqual');
const eqArrays = require('./eqArrays');
const assertArraysEqual = require('./assertArraysEqual');
/* INSTRUCTIONS: return all the indices (zero-based positions)
in the string where each character is found. */
const letterPositions = function(sentence) {
const results = {};
// loop through sentence
for ( let i = 0; i < sentence.length; i++){
//assigned sentence[i] to a variable called letter (e.g appears as 'h').
const letter = sentence[i];
//if this key does not exist in results yet
if(!results[letter]){
//adding letter to results object, e.g results with key of 'h' equals [0].
results[letter] = [i];
} else{
//if letter already appears, push the next value into existing array
results[letter].push(i);
}
}
return results;
};
module.exports = letterPositions;
// assertArraysEqual(letterPositions("hello").e, [1]);
// assertArraysEqual(letterPositions("hello"), true);