Here is a code snippet that explains what I am trying to do..
const names = ["foo", "bar", "baz"];
let actual = [];
// Implementation here..
const expected = ['f', 'o', 'o', ' ', 'b', 'a', 'r', ' ', 'b', 'a', 'z'];
// Tests
if (actual.length !== expected.length) {
console.error("Fail - Arrays not in equal length.");
}
for (let i = 0; i < expected.length; i++) {
if (actual[i] !== expected[i]) {
console.error('Fail - Letters are not equal in indices.');
break;
}
}
I do have an implementation for it but is there a simpler/elegant way to do this instead of using nested loops and an if condition to check to see if we are iterating over the last element?
Here what works for me which I do not like and want to improve:
for (const name of names) {
for (const letter of [...name]) {
actual.push(letter);
}
if (names[names.length - 1] !== name) {
actual.push(' ');
}
}