beginner javascript algorithm: method for this challenge?

donir

I'm doing this javascript algorithm challenge which has this array ['cat', 'game', 'socks'] and it has an image of what it must return:

[
  "******cat******",
  "*******game*******",
  "********socks********"
]

as you see, each word has a * more than the previous word, so not all words should be equal, would you use forEach? or how would you do it?

Also another question, I see my question is getting downvoted, If anybody sees something wrong, could you mention it please so I can fix it? Thanks in advance

Matthew

I was not going to originally submit an answer, but why not.

My approach would be using a for loop to iterate the list once and maintaining a string that gets incremented with a * for every for call.

In big-o notation, this would be an O(n) solution.

let data = ['cat', 'game', 'socks']
let buffer = '*****';

for(let i = 0; i < data.length; i++) {
  data[i] = `${buffer}\n*${data[i]}*\n${buffer}`;
  buffer += '*';
}

console.log(data)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related