Answer:
const solution = (arr) => {
const queue = arr
let res = "";
// keep looping untill the queue is empty
while(queue.length>0){
// taking the first word to enter the queue
const cur = queue.shift()
// get and add the first letter of that word
res+= cur.substring(0, 1);
// add the word to the queue if it exists
if(cur!== "") queue.push(cur.slice(1))
}
return res ;
}
console.log(solution(["Daily", "Newsletter", "Hotdog", "Premium"]));
// DNHPaeoriwtelsdmyloiegutmter
console.log(solution(["G", "O", "O", "D", "L", "U", "C", "K"]));
// GOODLUCK
Step-by-step explanation:
Using queue data structure in Javascript
This is the very optimal solution to the question