So I need to add different string variables to array and then split each of them by whitespace into array splitted[]
var books = new Array()
var a = "Lorem Ipsum dolor sit amet"
var b = "never gonna give you up"
var c = "another string variable"
books.push(a); books.push(b); books.push(c)
var splitted = []
for (let i = 0; i < books.length; i++) {
splitted = books[i].split(/[\s]+/)
}
console.log(splitted)
Expected result: (13)['Lorem', 'Ipsum', ... 'string', 'variable']
Actual result: (3)['another', 'string', 'variable']
So only the last variable c is splitted, while all three vars are in books[].
plitted = books[i].split(/[\s]+/)is the problem,you need to store all the result into a global arraysplittedis exactly that.push()to adda,bandctobooksbut then=in the loop. Why?.push()can handle multiple elements ->books.push(a, b, c)splitted[]global? I can also writesplitted.push(books[i].split(/[\s]+/))and output is three arrays inside thesplitted[]