-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 16 - InsertDash.js
54 lines (40 loc) · 1.11 KB
/
Day 16 - InsertDash.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
/*Insert Dashes
https://scrimba.com/scrim/co03e4eef94443d50bf215e1e
DESCRIPTION:
Transform a given sentence into a new one with dashes between each two consecutive letters.
Example:
For input = "aba caba", the output should be "a-b-a c-a-b-a"
Hints: join(), split()
*/
function isLetter(inputText) {
const regex = /^[A-Za-z]+$/
return regex.test(inputText)
}
function insertDashes(arr) {
let newString = ""
for (let i=0; i < arr.length; i++) {
// console.log(i + " " + arr[i] + " " + arr[i+1])
newString = newString + arr[i]
if (i < arr.length - 1 &&
isLetter(arr[i]) &&
isLetter(arr[i+1])) {
newString = newString + "-"
}
}
return newString
}
/**
* Test Suite
*/
describe('insertDashes()', () => {
it('insert dashes in between chars', () => {
// arrange
const value = "aba caba";
// act
const result = insertDashes(value);
// log
console.log("result: ", result);
// assert
expect(result).toBe("a-b-a c-a-b-a");
});
});