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.
 

38 lines
1.1 KiB

// Exercise 11: Functions
const printPerson = (name, age) => {
console.log(`Hi ${name}, you are ${age} years old.`);
}
printPerson('Zed', 44);
printPerson('Fran', 100);
printPerson('Alex', 30);
printPerson('Eve', 35);
console.log('--------------- pets ----------------');
const printPet = (owner_name, owner_age, pet_name, pet_age) => {
printPerson(owner_name, owner_age);
console.log(`That person owns ${pet_name} who is ${pet_age} years old.`);
}
printPet('Zed', 44, 'Mr. Scruffles', 10);
printPet('Fran', 100, 'Crazy', 2);
printPet('Alex', 30, 'Lizzy Lizard', 1);
printPet('Eve', 35, 'Kong The Donkey', 20);
// this part is tough! brain melting! give it a try
//
console.log('------------------ callback style ----------------');
const fancyPet = (owner_name, owner_age, pet_name, pet_age, cb) => {
cb(owner_name, owner_age);
console.log(`That person owns ${pet_name} who is ${pet_age} years old.`);
}
// notice how I use a function here as the parameter cb?
fancyPet('Zed', 44, 'Mr. Scruffles', 10, (name, age) => {
console.log(`Ooooh fancy ${name} you are ${age} old.`);
});