3

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'];
4
  • Create a new array const babies = []; then push() to it instead of logging. Commented Jul 20, 2020 at 0:13
  • 2
    Why don't you want to use map? Commented Jul 20, 2020 at 0:15
  • 1
    @girkovArpa It's for a Codecademy project not allowing methods! Commented Jul 20, 2020 at 1:33
  • So using .push() is allowed, I assume, given that it's still a method? Commented Jul 20, 2020 at 8:54

5 Answers 5

1

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);

Sign up to request clarification or add additional context in comments.

Comments

1

You can (ab)use reduce to return a new array matching the criterion you specified:

const array = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];

const newArr = array.reduce((newArr, anim) => !newArr.push('baby ' + anim) || newArr, []);

console.log(newArr);// ['baby panda', 'baby turtle', 'baby giraffe', 'baby hippo', 'baby sloth', 'baby human']

Comments

1

You can push the modified elements to a new array using forEach.

var convertToBaby = (array) => {
  const res = [];
  array.forEach(x => res.push('baby ' + x));
  return res;
};
const animals = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];
console.log(convertToBaby(animals));

2 Comments

Thank you @hev1 this is the code I ended up using! It makes sense to me. TY
@Carrie No problem.
1

You could use forEach or reduce to pass the new values to a new array without mutating the original array

const animals = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];
var newarray=[]
animals.forEach(element=>newarray.push("baby "+element))
console.log(newarray)

However if the reason why you don't want to use map it's because you don't want to make a new array and want to mutate the initial array you could splice each element and replace it with the new element but I don't recommend it

 const animals = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];
 for(let i=0;i<animals.length;i++){
        el="baby "+animals[i] 
        animals.splice(i,1)
        animals.unshift(el)
 }
console.log(animals.reverse())

2 Comments

It works, thank you! I'm not familiar with 'unshift' but I like it! TY
would you mind accepting my answer if it helped making your code work
0
const convertToBaby = arr => {
      const babyArray = [];
      for (let i = 0; i < arr.length; i++) {
            babyArray.push('baby ' + arr[i]);
      }
      return babyArray;
};

const animals = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];


convertToBaby(animals);

1 Comment

This ended up being the simplest code, a combination of a few ideas! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.