-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathwarmUp18.js
33 lines (29 loc) · 1000 Bytes
/
warmUp18.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
// You are given an input string.
// For each symbol in the string if it's the first character occurence, replace it with a '1', else replace it with the amount of times you've already seen it...
// But will your code be performant enough?
// Examples:
// input = "Hello, World!"
// result = "1112111121311"
// input = "aaaaaaaaaaaa"
// result = "123456789101112"
//for loop approach
function charCounter(string) {
//first character is not counted , first char is seen for first time.
var result = '1';
var splited = string.split('');
var seenCharCount = 1;
for (var i = 1; i < string.length; i++) {
//reseting the counter each character iterataion
seenCharCount = 1;
//iteration of previos value before the character
for (var j = i - 1; j >= 0; j--) {
//if confition if seen character is avaible previos values
if (splited[i] === splited[j]) {
seenCharCount++;
}
}
//adding the counter value to result to string
result+=seenCharCount;
}
return result;
}