Struggling to access an array and loop through it, but only able to print out one element instead of all. Need a hint on how to solve this issue.
See my code below:
let toppingsCount;
const burger = document.createElement('div');
const toppingsDiv = document.createElement('div');
const condiments = ['Ketchup', 'Mustard', 'Relish']
const condimentsNewDiv = document.createElement('div');
function createBurger(condiments){
burger.className = 'burger';
burger.appendChild(toppingsDiv)
toppingsDiv.className = "toppings";
condimentsNewDiv.className = "condiments";
toppingsDiv.appendChild(condimentsNewDiv)
for(let toppingsCount = 0; toppingsCount != condiments.length ; toppingsCount++){
toppingsDiv.appendChild(condimentsNewDiv).textContent = condiments[toppingsCount];
console.log(condimentsNewDiv);
}
console.log(burger);
document.body.appendChild(burger)
}
createBurger(condiments);
Inserted a console.log(condimentsNewDiv); inside the loop to check each condiment. However, it repeated "Relish" 3 times instead of printing out all three elements as expected:
<div class="condiments">Ketchup</div>
<div class="condiments">Mustard</div>
<div class="condiments">Relish</div>