Why does the code print both dots in the array simultaneously instead of printing dot, pausing, then printing dash, and finally printing dot? Is there an issue with my conditional statement?
function morseCode(code) {
if (code.length === 0) {
return;
}
let ele = code[code.length - 1];
if (ele === "dot") {
setTimeout(() => {
console.log("dot");
}, 100);
} else {
setTimeout(() => {
console.log("dash");
}, 300);
}
return morseCode(code.slice(0, -1));
}
let code = ["dot", "dash", "dot"];
morseCode(code);
// print 'dot'
// pause for 100ms
// print 'dash'
// pause for 300ms
// print 'dot'
// pause for 100ms