I have been trying to come up with a unique code based on the input provided.
Input = "ABC DEF GHI"
The generated code would look like,
"ADG" (first letter of each word) and if that is taken, then "ABDG" (first two letters of the first word and the first letter of the rest). If that's also taken, then "ABCDG", following by "ABCDEG", and so on until a unique code is created. The validation process is done through an API call.
The inputs can vary:
"ABC" (or) "ABC DEF" (or) "ABC DEF GHI" (or) "ABC DEF GHI JKL"
I attempted the following approach:
let count = 1;
let val = this.userForm.get("name").value;
let textArr = val.split(" ");
let res;
for(let i = 0; i< textArr.length; i++){
res += textArr[i].substring(0,count)
}
let char1 = "", char2 = "", char3 = "";
let ss:any = val.split(" ", 3);
if(ss[0].length > 1)
char1 = ss[0].substring(0,2)
if(ss[1] && ss[1].length > 0)
char2 = ss[1].substring(0,1)
if(ss[2] && ss[2].length > 0)
char3 = ss[2].substring(0,1)
let result = char1 + char2 + char3;
this.restApi.validateCode(result).subscribe((data: any) => {
console.log(data) // returns boolean
});
I'm struggling to achieve the desired logic. Any suggestions?