THE CODE DOWN BELOW IS A FINAL SOLUTION FROM CODECADEMY. I JUST NEED TO UNDERSTAND HOW IT WORKS.
Hello everyone, I’m having a hard time understanding this code from the practice. Can you please explain how this code works. Specifically, the IF ELSE part where the index is 0 and list length is 3 - 2 which is 1. So the iteration added a comma after the first string and when the second iteration goes the index is 1 and list length is still 3 - 2 which is 1 from here it goes to ELSE IF statement because previous one came out as FALSE. Here index is 1 and list length is still 3 - 2 is 1 that means it should add “AND” instead of ", ". I’m stuck on this part. (It would be great for CODECADEMY developers to include comment on their solutions for us students to understand their code)
Here is the instruction: Write a function groceries() that takes an array of object literals of grocery items. The function should return a string with each item separated by a comma except the last two items should be separated by the word 'and' . Make sure spaces ( ' ' ) are inserted where they are appropriate.
const groceries = list => {
let listString = ''
for (let i=0; i<list.length; i++) {
listString += list[i].item;
if (i < list.length - 2) {
listString += ', ';
} else if (i == list.length - 2){
listString += ' and ';
}
}
return listString;
}
groceries( [{item: 'Carrots'},
{item: 'Hummus'},
{item: 'Pesto'},
{item: 'Rigatoni'}] );
First question to ask is, are we mutating the array or in need of the index? In many cases neither, which is the case here. If all we want is access to the data, without a care for where it sits, then access the data points with of.
Here is the instruction: Write a function groceries() that takes an array of object literals of grocery items. The function should return a string with each item separated by a comma except the last two items should be separated by the word 'and' . Make sure spaces ( ' ' ) are inserted where they are appropriate.
Hopefully you have this by now - it gave me a headache as I was console.logging instead of returning!
The Codecademy solution is saying:
//Add the word, no matter where it is =>
listString += list[i].item;
//But if it's LESS than the second to last word always put a comma afterwards (Remember the last word would be Length-1)=>
if (i < list.length - 2) {
listString += ', ';
//Otherwise, if it is the second to last word, make sure you put 'and' after it.
} else if (i == list.length - 2){
listString += ' and ';
The response from @pjbentham helped me understand what is happening in the solution, but I want to make sure I understand the list[i].item part. Is the dot notation in this line of code just allowing us to create an object property in the list array?
//Add the word, no matter where it is =>
listString += list[i].item;
//But if it's LESS than the second to last word always put a comma afterwards (Remember the last word would be Length-1)=>
if (i < list.length - 2) {
listString += ', ';
//Otherwise, if it is the second to last word, make sure you put 'and' after it.
} else if (i == list.length - 2){
listString += ' and ';
list[i] must be an object with the property, ‘item’. We’d need to see the complete array to be certain, but it would not have .item if there was no item property.
My solution. Long code. The idea was not to specify the .item key specifically, in order for the function to be universal. The function takes the value of the first key in each object.
const groceries = (array) => {
let text = '';
array.forEach(object => {
if(object !== array[array.length-1] && object !== array[array.length-2]){
text = text + object[Object.keys(object)[0]] + ', ';
}
else if(object == array[array.length-2]){
text = text + object[Object.keys(object)[0]] + ' and ';
}
else{
text = text + object[Object.keys(object)[0]];
}
});
return text;
}
console.log(groceries( [{notitem: 'Carrots', packaging: 'bag'}, {item: 'Hummus'}, {item: 'Pesto'}, {item: 'Rigatoni'}] ));
If it helps you learn there is no fault in that. We learn by doing, regardless the interim outcomes.
The idea was and is to be able to access the same key in different objects, which is how they relate to each other. They are not just members in the same list, they also share the same properties.
Challenging, this one! Noticed I resorted to extracting the values into an array instead of working with the objects themselves - it wasn’t a conscious choice, but I’ll keep these things in mind. Here’s my solution:
// Write function below
const groceries = arr => {
const array = [];
arr.forEach(obj => {
array.push(obj.item);
});
let str = '';
for (let i=0; i < array.length; i++) {
if (i === 0) {
str += array[0];
} else if (i === (array.length-1)) {
str += ' and ' + array[i];
} else {
str += ', ' + array[i];
}
}
return str;
}
console.log(groceries([{item: 'Carrots'}, {item: 'Hummus'}, {item: 'Pesto'}, {item: 'Rigatoni'}]));