2

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.

1
  • Could you provide example output as well? Commented Feb 12, 2018 at 10:36

1 Answer 1

3

You are not saving the value back to newString (concat doesn't mutate the string value), replace it by

newString = newString.concat(temp);

and

newString = newString + word;//outside if

Also you can't re-assign a value to const, so replace

const newString = ''

with

var newString = ''

Demo

function test(args){
  const numb = args.K
  const word = args.S
  var newString = ''

  for(let i=0;i<word.length;i++){
    debugger
      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 = 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 = newString.concat(temp)
          continue;
      }
      newString = newString + character; 
  }
  return newString
}

console.log(test({
  K: 11,
  S: 'Hello - World'
}));

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, yes the last bit was meant to be newString = newString + character
@William What is the expected output?
Another small error, I put indexOfChar -= 25; instead of indexOfChar -= 26; for the input recommended we expected: ""Spwwz - Hzcwo""
@William Check the edited code, you need to keep newString = newString + character;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.