Write a function that replaces the words in raw
with the words in code_words
such that the first occurrence of
each word in raw
is assigned the first unassigned word in code_words
.
Assume that the total number of unique elements in raw
string is greater than the total number of unique elements in code_words
encoder([“a”], [“1”, “2”, “3”, “4”]) → [“1”]
encoder([“a”, “b”], [“1”, “2”, “3”, “4”]) → [“1”, “2”]
encoder([“a”, “b”, “a”], [“1”, “2”, “3”, “4”]) → [“1”, “2”, “1”]
encoder([“a”, “b”, “a”, “c”], [“1”, “2”, “3”, “4”]) → [“1”, “2”, “1”, “3”]
my algorithm is:
- create a function that takes two arrays as an argument
- create newArray to hold the return
- convert the array to two dimensional array;
- loop through it and map each element with the other
- push the mapped element into newArray created in step 2
- return the newArray
but it is not working as it intends to do and am a newbies, can somebody help me out?
this my code, somebody should sincerely help me out
const list1 = ["a", "b", "a", "c"];
const list2 = ["1", "2", "3", "4"];
function encode(str1, str2){
let arr2 = [];
arr2.push(str1, str2);
let newArr = [];
for (var i = 0; i < arr2[0].length; i++){
for(let j = 0; j <= i; j++){
if(filt.includes(arr2[0][i]) ){
// if(str[0][i] === str[1][j]){
newArr.push(list2.charAt(j) )
}
else{
newArr.push(list2.charAt(j))
}
}
}
return newArr
}
console.log(encode(list1, list2));