I'm trying to create a unique key using the alphabets; I've managed to work it out but I don't really like the approach I took. and I believe someone out there might be able to help me get a better way to do this code. Please let me know if you think of a better idea? or whatever your idea to compare with mine? I think this might be a good way to share knowledge .. Thanks in advance
let string = 'ABE';
let ar1 = ["A", "B", "C", "D", "E"];
string = string.split('');
if (string[2] !=='E') {
for (let i = 0; i < 5; i++) {
if (string[2] === ar1[i]) {
i = i+1;
string[2] = ar1[i]
}
}
string = string.toString().replace(/,/g, '');
console.log("case 1", string);
} else if (string[1] !=='E' && string[2] ==='E') {
string[2] ='A';
for (let i = 0; i < 5; i++) {
if (string[1] === ar1[i]) {
i = i+1;
let newString = ar1[i];
console.log(ar1[i]);
string[1] = newString;
}
}
string = string.toString().replace(/,/g, '');
console.log("case 2", string);
} else if (string[1] ==='E' && string[2] ==='E') {
string[1] ='A';
string[2] ='A';
for (let i = 0; i < 5; i++) {
if (string[0] === ar1[i]) {
i = i+1;
string[0] = ar1[i];
}
}
string = string.toString().replace(/,/g, '');
console.log("case 3", string);
}
Results would be like:
if string = "AAA" Then the result "AAB"
if string = "AAE" Then result will be "ABA"
if string = "AEE" Then result will be "BAA"