0

I'm new to JS and I tried to make a basic piece of code for unscrambling words:

const nums = [3, 1, 0, 2]
let words = ["D", "B", "A", "C"]
let finalString = ["", "", "", ""]

console.log(finalString)

for (let i = 0; i < 4; i++) {
  finalString.splice(nums[i], 1, words[i]);
}

console.log(finalString.join(" "))

For some reason, the console outputs ["A", "B", "C", "D"] for the code on line 5, when I assume it should print the what I defined finalString as the line before.

Does anyone have any idea why this happens? or am I missing something obvious.

I'm using repl as an IDE if that changes anything.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
Zain
  • 1

0 Answers0