FAQ: Code Challenges: Intermediate JavaScript - convertToBaby()

your function logs the newArray, it doesn’t return the newArray

also, why use an if condition? You can simple return the array after the loop

I feel like no one likes to use the .reduce() function. Maybe because it is harder to understand.
This is my result:

const convertToBaby = array => array.reduce((newArray, element) => {
  newArray.push(`baby ${element}`)
  return newArray;
}, []);

Why does this return undefined when running? https://gist.github.com/e44bc4e65ca345a9550f08b0faca7306

Nevermind on this I figured out what I did wrong. I forgot to return the everything from my function. there for when I logged to the console it didn’t have anything to log as it was hidden inside my function.

It’s a bit frustrating that I get the correct results but it won’t let me move forward.
I cannot figure out why it thinks there is a bug. After 15 minutes, I look at the solution.

Oh, I used a string literal instead of concatenation.

The instructions didn’t say use concatenation specifically, so that was kind of a waste of time.

Hello can somebody explain to me why my loop works but after the last result i get also an undefined?

// Write your code here:
const convertToBaby = (array) => {

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

console.log(`baby ${array[i]}`);

}
}
const animals = [‘panda’, ‘turtle’, ‘giraffe’, ‘hippo’, ‘sloth’, ‘human’];
console.log(convertToBaby(animals))

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

Thank you!

Nevermind, I got it , i completely misread the instructions :slight_smile:
Anyways this is my code:

const convertToBaby = (array) => {

let babyAnimals = ;

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

babyAnimals.push(`baby ${array[i]}`);

} return babyAnimals

}

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

console.log(convertToBaby(animals))

Hello,

i try do to this exercice with the .map method. Anybody had an idea why my code is returning the good result but “undefined” is hqd before my array?


const convertToBaby = array => {
  newArray = array.map(i => `baby ${i}`)
  console.log(newArray)
}
[ 'baby panda',
  'baby turtle',
  'baby giraffe',
  'baby hippo',
  'baby sloth',
  'baby human' ]
undefined

thanks

the convertToBaby function itself does not return anything. An absence of return value could result in an undefined

cool; i change my console.log for a return.

thanks for your help

reduced solution is nice!
more simple despite with on more line of code

const convertToBaby = (arr) => {
  let tempArr = [];
  arr.forEach(word => {
    word = "baby " + word;
    tempArr.push(word);
  });
  return tempArr;
}

// Write your code here:
function convertToBaby(animals) {
for(let i = 0; i < animals.length; i++) {
animals[i] = 'baby ’ + animals[i];
}
return animals;
}

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

function convertToBaby(animals) {
for(let i = 0; i < animals.length; i++) {
animals[i] = 'baby ’ + animals[i];
}
return animals;
}

What about these code dumps?

For my code below, why are ‘arr’ and ‘animals’ interchangeable on the lines I commented on? Does it matter which one is used?

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

const convertToBaby = (arr) => {
  const babyArray = []
  for (let i = 0; i < arr.length; i++) {             //can change 'arr' with 'animals' and get the same result. Why?
    babyArray.push(`baby ${arr[i]}`);           //same here, why?
      
}
    return babyArray;
}

console.log(convertToBaby(animals))

Thank you.