Function groceries()

This code has all the expected output but is not being passed. Any suggestions.

// Write function below
const groceries = array => {
  
  if (array.length >=3){
  let list = array.map( food =>food.item
  )
  list.splice(list.length-1, 0, 'and');
  let string = list.join(', ').replace('and,', 'and')
  return string;
  }


  if (array.length ==2 ){
  let list = array.map( food =>food.item
  )
  list.splice(list.length-1, 0, 'and');
  let string = list.join(' ')
  return string;
  }

  if (array.length ==1 ){
  let list = array.map( food =>food.item
  )
  
  let string = list.join(' ');
  return string;
  }


}

console.log(groceries( [{item: 'Carrots'}, {item: 'Hummus'}, {item: 'Pesto'}, {item: 'Rigatoni'}] ));
// returns 'Carrots, Hummus, Pesto and Rigatoni'
 
console.log(groceries( [{item: 'Bread'}, {item: 'Butter'}] ));
// returns 'Bread and Butter'
 
console.log(groceries( [{item: 'Cheese Balls'}] ));
// returns 'Cheese Balls'

console.log(groceries([{item: 'Lettuce'}, {item: 'Onions'}, {item: 'Tomatoes'}]));


Could you describe the CC “error” message being thrown?

Here’s my solution,

Edit
Actually just found a problem, so I guess I’m trying to sortthat out

edit 2
let firstBit = tempArray.slice(0, tempArray.length - 3).join(", "); changed -3 to -2, sorted


const list1 = [

  { item: "Carrots" },

  { item: "Hummus" },

  { item: "Pesto" },

  { item: "Rigatoni" },

  { item: "thing" },

  { item: "stuff" }

];

const list2 = [

  { item: "Bread" }, 

  { item: "Butter" }

];

const list3 = [

  {item: 'Cheese Balls'}

];

function shoppingList(array) {

  let tempArray = [];

  for (let { item } of array) {

    tempArray.push(item);

  }

  if (tempArray.length < 2) {

    return tempArray.join();

  } else if (tempArray.length === 2) {

    return tempArray.join(" and ");

  } else {

    let lastTwo = tempArray.slice(tempArray.length - 2).join(" and ");

    let firstBit = tempArray.slice(0, tempArray.length - 3).join(", ");

    return firstBit.concat(", ", lastTwo);

  }

}

console.log(shoppingList(list1));

console.log(shoppingList(list2));

console.log(shoppingList(list3));

Hi all,

Struggling with this one; I can’t figure out why my array won’t join up into a single string at the end? Here’s my code:

const groceryList = [{item: 'Carrots'}, {item: 'Hummus'}, {item: 'Pesto'}, {item: 'Rigatoni'}];

// Write function below

const groceries = objArr => {

  const newArr = [];

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

      if (i == (objArr.length - 2)) {

        newArr.push(objArr[i].item);

        newArr.push('and');

      } else {

        newArr.push(objArr[i].item);

      }

    };
  newArr.join(', ') ;

  return newArr;

};

console.log(groceries(groceryList));

Which just returns:
[ ‘Carrots’, ‘Hummus’, ‘Pesto’, ‘and’, ‘Rigatoni’ ]

.join() appears to return an object, so instead of directly changing newArr to be a string, .join() create a new object, a string, which is newArr joined with commas. Right now, what you’re doing is like writing:

let somevalue = 0;
5;
console.log(somevalue);

and expecting it to output 5.

A hint on how to fix this-only look at this if you're still stuck after reading my explanation

Since newArr.join(", ") returns something, could you not just return that expression?

`return newArr.join(", ")

1 Like