I am to finish the function below. I am to loop through the arr paramteter using a for loop and add the string "Duck" to the end of each element (i.e. arr[0] = "yellow"; should become "yellowDuck".
Here is what I am given to start with:
function addDucks(arr, ind) {
//WRITE YOUR FOR-LOOP HERE
//For your iterator, declare it with the let keyword, and name it "i"
//DO NOT TOUCH THIS
return [arr, ind]
}
Here is the code I am trying:
function addDucks(arr, ind) {
for (let i = 0; i < arr.length; i++) {
return arr[i] + 'Duck';
}
return [arr, ind]
}
1 Answer 1
Your code was close, you were just not changing the reference in the array to be the string with Duck added. Modified the return arr[i] + 'Duck' to arr[i] += 'Duck' which is the same as arr[i] = arr[i] + 'Duck'
function addDucks(arr, ind) {
for (let i = 0; i < arr.length; i++) {
arr[i] += 'Duck';
}
return arr;
}
let ducks = addDucks(['green','purple'], 2);
console.log(ducks);
answered Jun 4, 2018 at 22:52
pmkro
2,5382 gold badges19 silver badges25 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
should return an array the first element of the returned array should be the passed-in array with "Duck" added to every element the second element of the returned array should be 3 when passed [1, 2, 3], 3