-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 17 - DifferentSymbolsNaive.js
72 lines (51 loc) · 1.51 KB
/
Day 17 - DifferentSymbolsNaive.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*Different symbols Naive
https://scrimba.com/scrim/coadc4ae4a9ce2fc50709ca5d
DESCRIPTION:
Given a string, find the number of different characters in it.
Example:
For s = "cabca", the output should be 3
These are 3 different characters: a, b and c.
Hints: includes(), split(), push()
*/
function differentSymbolsNaive(str) {
let splitStr = str.split("")
let unique = splitStr.length
for(i=0; i < splitStr.length; i++) {
// console.log("unique:" + " " + unique)
// console.log(splitStr[i])
// console.log("slice:" + " " + splitStr.slice(i+1))
if (splitStr.slice(i+1).includes(splitStr[i])) {
// console.log(splitStr.slice(i+1) + " includes " + splitStr[i])
unique = unique - 1
}
}
return unique
}
/* or:
function differentSymbolsNaive(str) {
let splitStr = str.split("")
let diffChar = {}
for(i=0; i < splitStr.length; i++) {
if (diffChar[splitStr[i]] === undefined) {
diffChar[splitStr[i]] = 0
}
diffChar[splitStr[i]]++
}
console.log(diffChar)
return Object.keys(diffChar).length
*/
/**
* Test Suite
*/
describe('differentSymbolsNaive()', () => {
it('returns count of unique characters', () => {
// arrange
const str = 'cabca';
// act
const result = differentSymbolsNaive(str);
// log
console.log("result: ", result);
// assert
expect(result).toBe(3);
});
});