FAQ: Code Challenges: Intermediate JavaScript - convertToBaby()

Thank you :slight_smile:

Again, correct code doesn’t pass the code checker :roll_eyes:

// Write your code here:
const babyArr = [];
const convertToBaby = (arr)=>{
  for(let i=0;i < arr.length; i++){
    babyArr.push(`baby ${arr[i]}`);
  };
  return babyArr;
}

// When you're ready to test your code, uncomment the below and run:

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

console.log(convertToBaby(animals)) 

This code is not correct, if we add another function call:

console.log(convertToBaby(animals)) 
console.log(convertToBaby(['a', 'b', 'c'])) // I was too lazy to use actual animal names

we get the wrong output.

2 Likes

Got it. I have the new array in the global scope. Thank you.

1 Like

return exits the for loop

Hello there! Could you please answer these questions for me? Something confuses me very badly!

Why did you write within the body of your for loop this thing: 'animals[i] = ‘baby ’ + animals[i];’?, instead of just writing 'return ‘baby ’ + animals[i];’.

I saw it works, but I am just not sure why write it that way. I mean, if you have that [i] next to the ‘animals’ parameter/variable, can it even be used as a re-assignment option anymore? And if so, then why?

Thank you and sorry if I can’t make sense in my head with this. I have ADHD and its though to see non-linear logic.

// Write your code here:

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

let now = ;

const convertToBaby = (item) =>{

for(let i = 0; i < item.length; i++){

now.push('baby ' + item[i])

}

return now;

}

// When you’re ready to test your code, uncomment the below and run:

console.log(convertToBaby(animals))


Correct code, yet still cannot pass the excercise. It looks identical to the proposed solution (minus the variable names). What’s wrong with this?

I think your code is correct but you just need to have ‘Baby’ as lowercase: 'baby ’

Hi all,

I am not sure how this isn’t looping through with a for…in loop. If I add an empty array and then try to return the results to that array, I still get the same output. I used the hint, which gives you a for loop using i but the output was the same. Thanks in advance. Gist and output below:

Gist: Codecademy export · GitHub

Output: baby panda

return does what it says: returning/handing back data

when a return keyword is reached, the data is returned to the function call, which signals that the function is done executing

so even when the return keyword is in the loop, the remaining iterations of the loop are not executed

Ok that makes sense. I used return because the problem said to use return but contextually understanding what the requirement is also important. I just used console.log instead of return and I am not getting an array but a list and it ends with undefined. My first move is to add a results array but I tried that earlier and it gives me the same list and an empty array at the end.

Output:

baby panda
baby turtle
baby giraffe
baby hippo
baby sloth
baby human
undefined

if you want to end up with an array, you will need to .push() elements into an array and then return the array (after/outside the loop)

I did that right after I sent my previous reply and it worked. Thanks again for your help!

Ok, I’m totally confused as to why we are using .push() to get babyAnimal. The def for the push method is to add an item to the end of an array and change the length of the array. We are putting baby in front of the item in the array, and the array length is not being changed (just overall lengthened by adding baby to each item).

we are creating a new array to contain the baby version of each animal

The .map() Method takes an argument of a callback function and returns a new array!


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

const convertToBaby = animals.map(singleAnimals =>'baby ' + singleAnimals);

/*const convertToBaby = arr => {
  const babyArray = [];
  for (let i = 0; i < arr.length; i++) {
      babyArray.push('baby ' + arr[i]);
  }
  return babyArray
}*/


console.log(convertToBaby) 

I was able to get the expected result but the system did not mark my code as correct what am I missing here?

let babyAnimal = new Array;
let convertToBaby = animal => {
for(let i = 0; i < animal.length; i++) {
babyAnimal.push(baby ${animal[i]});
}
return babyAnimal;
}
returned: [ ‘baby panda’,
‘baby turtle’,
‘baby giraffe’,
‘baby hippo’,
‘baby sloth’,
‘baby human’ ]

got it it works now. after reading @jeffa comments I moved my blank array in the function. It has the same result but now the system, took it

Hello, your example really helped me figure it out! I was missing the str concatenation.
I noticed you also made a simple error. Using the literal array as an argument in the function.
##I refactored it a bit, and it works:

const convertToBaby = arr => {
let newArray = ;
for (let i=0; i < arr.length; i++) {
newArray.push('baby ’ + arr[i]);
console.log(newArray);

} return newArray;

};