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));