I am facing an issue with a function that I have written
function collectData() {
var data = [];
data[0] = {name: "Sophia", age: 25, gender: "female"};
data[1] = {name: "John", age:30, gender: "male"};
data[2] = {name: "Emily", age: 22, gender: "female"};
data[3] = {name: "Michael", age: 35, gender: "male"};
data[4] = {name: "Sarah", age: 28, gender: "female"};
}
In my code body, I attempted to use this:
var data = collectData();
var targets = ["John", "Sarah"];
for(var i = 0; i < data.length; i++){
var spanTag ="<span>";
However, I encountered an error stating that the variable is undefined when I try to loop through it. I am confused as I believe I have defined it by calling the function and storing the result in a variable.