-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringChallenge.js
More file actions
78 lines (63 loc) · 2.06 KB
/
Copy pathstringChallenge.js
File metadata and controls
78 lines (63 loc) · 2.06 KB
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
68
69
70
71
72
73
74
75
76
77
/*Have the function StringChallenge (str, num) take the str parameter and perform a
Caesar Cipher shift on it using the num parameter as the shifting number. A Caesar Cipher
works by shifting each letter in the string N places in the alphabet (in this case N will be num)
Punctuation, spaces, and capitalization should remain intact. For example if the string is
"Caesar Cipher' and num is 2 the output should be, "Ecguct Ekrigt
Once your function is working, take the final output string and intersperse it character-
by-character with your ChallengeToken
Your ChallengeToken: hbpu1f5087
Examples
Input:
"'Hello" & num = 4
Output: Lipps
Final Output: Lhibpppus1f5087
A
Input: "abc" & num = 0
Output: abc
Final Output: ahbbcpu1f5087*/
// help me solve this string challenge
function StringChallenge(str, num) {
// code goes here
let newStr = '';
for (let i = 0; i < str.length; i++) {
if (str[i] === ' ') {
newStr += str[i];
} else {
newStr += String.fromCharCode(
str.charCodeAt(i) + num
);
}
}
let challengeToken = 'hbpu1f5087';
function findDiff(str, token) {
let diff;
if (token > str){
diff = token.length-str, str.length;
} else if(token < str){
diff = str. length-token, str.length;
} else{
diff = '';
}
return diff;
}
//
//Once your function is working, take the newStr and intersperse it character- by-character with your ChallengeToken
// if it's Hello, num = 4, then newStr = Lipps, then newStr = Lhibpppus1f5087 : hbpu1f5087
let remainingToken = challengeToken.slice(-findDiff(newStr, challengeToken));
function intersperse(str, token) {
let newStr = '';
for (let i = 0; i < str.length; i++) {
newStr += str[i];
if (i < str.length - 1) {
newStr += token;
}
}
return newStr+remainingToken;
}
return intersperse(newStr, challengeToken);
}
console.log(StringChallenge("'Hello'", 4));
// how to pop off the last
let challengeToken = 'hbpu1f5087';
let last4LettersofChallengeToken = challengeToken.slice(-4);
console.log(last4LettersofChallengeToken);