newbie here.
I'm having trouble with a simple challenge, the goal is simple, push the sedan cars into another array, so the output when the function is called (no console.log can be used, it will be ignored) is:
[ { type:'sedan', size: 'white'} , { type:'sedan', color:'red'} ].
The suggestion is to use push().
Here is my terrible code.
Any help is welcome!
function filterCars (cars) {
const sedanCars = []
const carArr = [
{ type: 'sedan', color: 'white'},
{ type: 'truck', color: 'blue'},
{ type: 'sedan', color: 'red'},
{ type: 'coupe', color: 'red'}
]
var arrayLength = carArr.length;
for (var i = 0; i < arrayLength; i++) {
console.log(carArr[i]);
if(carArr.type==="sedan") {
return sedanCars.push("sedan");
}
Your filterCars function and return do not seem all that clear to me in their function, especially since you are triggering your return within a for loop iteration.
There are several ways we can approach this…
filter-based functionfunction getSedans(cars) {
return cars.filter(car => car.type === 'sedan')
}
const carArr = [
{ type: 'sedan', color: 'white' },
{ type: 'truck', color: 'blue' },
{ type: 'sedan', color: 'red' },
{ type: 'coupe', color: 'red' }
];
const sedanCars = getSedans(carArr);
// including this console.log to provide the output
console.log(sedanCars);
filter-based function + push() & spread (...)If you would prefer to use this solution but also use push, you could do by declaring the empty sedanCars array before the carArr and then push the results of getSedans(carArr) to it like this…
const sedanCars = [],
carArr = [ /* cars here */ ];
sedanCars.push(...getSedans(carArr));
You can see that in action here:
function getSedans(cars) {
return cars.filter(car => car.type === 'sedan')
}
const sedanCars = [],
carArr = [
{ type: 'sedan', color: 'white' },
{ type: 'truck', color: 'blue' },
{ type: 'sedan', color: 'red' },
{ type: 'coupe', color: 'red' }
];
sedanCars.push(...getSedans(carArr));
// including this console.log to provide the output
console.log(sedanCars);
push() method using for..of loopHowever, this adds an extra unnecessary step by using spread. If you really prefer to use push(), here is a more appropriate method for using it:
const sedanCars = [],
carArr = [
{ type: 'sedan', color: 'white' },
{ type: 'truck', color: 'blue' },
{ type: 'sedan', color: 'red' },
{ type: 'coupe', color: 'red' }
];
for (const car of carArr) {
if (car.type === 'sedan') sedanCars.push(car);
}
// including this console.log to provide the output
console.log(sedanCars);
push() method using forEach methodAnd lastly, if you wanna get fancy, you could replace that entire for..of loop with a forEach invocation and replace the if statement for a short circuit, like this:
carArr.forEach(car => car.type === 'sedan' && sedanCars.push(car));
Here is how that would look in action:
const sedanCars = [],
carArr = [
{ type: 'sedan', color: 'white' },
{ type: 'truck', color: 'blue' },
{ type: 'sedan', color: 'red' },
{ type: 'coupe', color: 'red' }
];
carArr.forEach(car => car.type === 'sedan' && sedanCars.push(car));
// including this console.log to provide the output
console.log(sedanCars);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With