Please, help me write the same JavaScript function with less code

Hello guys, I hope you are having a beautiful day, and if there’s any one of you out there who is coding during the after Christmas day, then Kudos XD.

I need your help. Well, I wrote this beautiful function down here and it’s working pretty well, but I’m afraid my code looks a bit longer and inaccurate than it’s supposed to be.

How can I shrink it? How can I write the same function with less code?

(Here’s the actual task: write a function responsible for randomly selecting a letter in the given array and changing the current letter to a different letter. Then, the function will return the changed letter.

GIVEN ARRAY: const givenArray = [β€˜A’, β€˜T’, β€˜C’, β€˜G’];

For example, if the randomly selected letter is β€˜A’, the letter be changed to β€˜T’, β€˜C’, or β€˜G’. But it cannot be β€˜A’ again.) Many many thanks in advance !!!

const changeCode = input => {

newArray = [β€˜A’, β€˜T’, β€˜C’, β€˜G’];

if(input === β€˜A’) {

 input = newArray[Math.floor(Math.random()*4)];

 if(input === 'A') {

    input = newArray[Math.floor(Math.random()*4)];

  }

  return input;

} else if (a === β€˜T’) {

 input = newArray[Math.floor(Math.random()*4)];

 if(input === 'T') {

  input = newArray[Math.floor(Math.random()*4)];

}

return input;

} else if (a === β€˜C’) {

 input = newArray[Math.floor(Math.random()*4)]; 

 if(input === 'C') {

  input = newArray[Math.floor(Math.random()*4)];

}

return input;

} else if (a === β€˜G’) {

 input = newArray[Math.floor(Math.random()*4)];

 if(input === 'G') {

  input = newArray[Math.floor(Math.random()*4)];

}

return input;

}

}

let a = β€˜T’;

console.log(changeCode(a));

Hi there! You might want to take a look at this FAQ How Do I Format Code In My Posts for next time so that it will look this :smiley:

const changeCode = input => {
   newArray = [β€˜A’, β€˜T’, β€˜C’, β€˜G’];

   if(input === β€˜A’) {
      input = newArray[Math.floor(Math.random()*4)];
      return input;

   } else if (a === β€˜T’) {
      input = newArray[Math.floor(Math.random()*4)];
      return input;

   } else if (a === β€˜C’) {
      input = newArray[Math.floor(Math.random()*4)]; 
      return input;

   } else if (a === β€˜G’) {
      input = newArray[Math.floor(Math.random()*4)];
      return input;
   }
}

let a = β€˜T’;
console.log(changeCode(a));
  1. Why are you comparing input === 'A' in your first if statement, but the others use a === ? Since you define a = 'T' and then use it as an parameter for your function, it would be more consistent to use input every time.
  1. You could use a switch instead if you want to make it look more concise. JS switch documentation
  2. Look at your code and think of DRY (Don’t Repeat Yourself). See how you are calculating a random number for every if statement? Why not just calculate it once.
  3. You are using Math.floor(Math.random() * 4) no matter which letter is matched in your if statement. This doesn’t guarantee that you won’t pick the index of the letter you found and repeat yourself.
  4. input is the argument of your function. Why are you redefining it?