How would I loop through an array in Javascript, add a word, and then return an array (without using map)?
var convertToBaby = (array) => {
for (var i =0; i < array.length; i++) {
console.log('baby '+ array[i]);
}
};
const animals = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];
convertToBaby(animals);
/*Returns
baby panda
baby turtle
baby giraffe
baby hippo
baby sloth
baby human */
// Should return ['baby panda', 'baby turtle', 'baby giraffe', 'baby hippo', 'baby sloth', 'baby human'];
maybe this
var convertToBaby = (array) => {
let arr = [];
for (var i =0; i < array.length; i++) {
arr.push('baby '+ array[i])
}
console.log(arr);
return arr;
};
const animals = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];
convertToBaby(animals);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With