-
Notifications
You must be signed in to change notification settings - Fork 0
/
sherlockString.js
59 lines (48 loc) · 1.51 KB
/
sherlockString.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
/*
checks to see whether a string is valid in the eyes of Sherlock;
A valid string has characters that show up the same amount of times;
Also, A valid string has all characters appear the same amount of times if
the character at index [1] is remeoved;
I: string;
O: boolean, but also print out YES or NO;
C:
E:
*/
var sherlockString = function(sherlock) {
var letters = {};
var sherlockApproval = 'YES';
var allotment = false;
for (var i = 0; i < sherlock.length; i++) {
if (!letters[sherlock[i]]) {
letters[sherlock[i]] = 1;
} else {
if (allotment) {
sherlockApproval = 'NO';
}
allotment = true;
}
}
console.log(sherlockApproval);
return sherlockApproval;
}
var assert1 = function (expected, description) {
var word = 'abc';
var actual = sherlockString(word)
console.log(description);
console.log(expected === actual);
}
assert1('YES', 'Given character occurance of 1 accross the board it should return YES');
var assert2 = function (expected, description) {
var word = 'abcc';
var actual = sherlockString(word)
console.log(description);
console.log(expected === actual);
}
assert2('YES', 'Given a string that when a character is remeoved it occurs the same number times each char');
var assert3 = function (expected, description) {
var word = 'wahooo';
var actual = sherlockString(word)
console.log(description);
console.log(expected === actual);
}
assert3('NO', 'Given a string that even when a character is removed wont have all characters occuring the same amount of times');