-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumbleHelper.js
More file actions
37 lines (33 loc) · 785 Bytes
/
JumbleHelper.js
File metadata and controls
37 lines (33 loc) · 785 Bytes
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
function Shift(ch, N) {
N=parseInt(N);
asciiCode=parseInt(ch.charCodeAt(0));
zAscii=parseInt('z'.charCodeAt(0));
if (asciiCode+N<=zAscii){
return String.fromCharCode(asciiCode+N)
}
else{
return String.fromCharCode( asciiCode -zAscii +'a'.charCodeAt(0)+ N-1)
}
}
module.exports.Jumble=function(strn, N) {
if(!(typeof strn === 'string' || strn instanceof String)) {
return "Error: Not a string";
}
if(isNaN(Number(N,10))) {
return "Error: Not Number"
}
var n=Number(N, 10);
if (n<1 || n>1000){
return "Error: N should range between [1-1000]";
}
var retArray ="";
for(i=0;i<strn.length;i++){
if(strn[i]>='a'&&strn[i]<='z'){
retArray+=Shift(strn[i],N%26);
}
else if(strn[i]>='0'&&strn[i]<='9'){
retArray+=strn[i]
}
}
return retArray;
}