I need to know how I can split an array with commas and if it's the last one element to the last one I have , and and if it's the last one I add . to it. I have the following code. The output as one big string honda, audi, tesla, and toyota. the following is printing more than one. Also what if I wanted to push it one time into new array. So one big array of string ["honda, audi, tesla, and toyota."]
let namesArray = ["honda", "audi", "tesla", "toyota"];
let newOrder = ""
namesArray.forEach((item, index) => {
console.log(index);
if (index === namesArray.length - 2) {
newOrder = item + ' , and'
} else if (index === namesArray.length - 1) {
newOrder = (item + '.')
} else {
newOrder = (item + ',')
}
})
console.log('new string ', newOrder)
"new string ", "honda,audi,tesla,toyota."
"and "and"."to the last element, then usearr.join(", ")newOrder += item + ....newOrder.append(item + ' , and')I get Uncaught TypeError: newOrder.append is not a functionnewOrder.append. I said usenewOrder +=instead ofnewOrder =.