aljf
1
Hey guys I was trying to do a coding challenge recently and I’m trying to figure out how to add a string item into an existing array. For example:
var strArray = ["Mountain", "Pepsi", "Coke"]
How would I add the string “Dew” at the end of the first element in the array?
What do you mean by at the end of the first element
?
You can add a string at the end of an array by using .push()
(if this is what you mean):
strArray.push('Dew');
aljf
4
I’m sorry I wasn’t clear I want to transform this:
var strArray = ["Mountain", "Pepsi", "Coke"]
Into this:
var strArray = ["Mountain Dew", "Pepsi", "Coke"]
It’s easy to do that.
You just need to change the string in question.
var arr = ["Mountain", "Pepsi", "Coke"];
arr[0] = 'Mountain Dew'; // Mountain is at index 0.
console.log(arr); // output: Mountain Dew,Pepsi,Coke