The code from the Learn JavaScript the Hard Way module JavaScript Level 1 exercises. This is a mirror of the code I have in the book, so if you're struggling you can use this to compare against your attempts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

48 lines
956 B

let pets = [
{name: 'Yeller', type: 'Dog', age: 12},
{name: 'Akumano', type: 'Japanese Bobtail Cat', age: 2},
{name: 'Meaw Peesard', type: 'Siamese Cat', age: 100},
{name: 'James', type: 'Gecko', age: 2},
]
const young_pets = (pet) => {
return pet.age <= 10;
}
const age_pets = (pet) => {
return [pet.name, pet.age + 10];
}
const name_age = (pet) => {
return `${pet[0]} is ${pet[1]}`;
}
const debug = (msg) => {
console.log(msg);
return msg;
}
const tee = (result, data, cb) => {
const side = (input) => {
cb(input, result, data);
return input;
}
return side;
}
let owned_pets = [];
const add_owner_tee = tee(owned_pets, 'Zed', (i, r, d) => {
r.push({pet: i, owner: d});
});
let age_young_animals = pets.filter(young_pets)
.map(add_owner_tee)
.map(age_pets)
.map(name_age);
console.log("-- Pets with Owners:");
owned_pets.forEach(debug);
console.log("-- Young Animals:");
age_young_animals.forEach(debug);