Unable to make String Concat work
In the code below I am not able to concat the variable temp to the variable newString. I have tested and seen that this is not an issue of scoping but I am still puzzled by this behavior.
Understanding the objective of the code
Below you can see an example of the code which takes an object with K (a number) and S (a string). The function is meant to shift the character's position in the alphabet by the number K while keeping itself as Upper or Lowercase for a given String S.
function shiftCharByInt(args){
const numb = args.K
const word = args.S
let newString = ''
for(let i=0;i<word.length;i++){
let character = word.charAt(i)
if(/^[a-z]+$/.test(character)){
let indexOfChar = character.charCodeAt(0)
indexOfChar+=numb
while(indexOfChar > 122) indexOfChar -= 26;
let temp = String.fromCharCode(indexOfChar)
newString.concat(temp)
continue;
}
if(/^[A-Z]+$/.test(character)){
let indexOfChar = character.charCodeAt(0)
indexOfChar+=numb
while(indexOfChar > 90) indexOfChar -= 26;
let temp = String.fromCharCode(indexOfChar)
newString.concat(temp)
continue;
}
newString += word
}
return newString
}
Example Input:
{
K: 11,
S: 'Hello - World'
}
Example Output:
"Spwwz - Hzcwo"
Again, I am more interested in understanding why concat does not work optimising the code itself.