Skip to content

Commit

Permalink
end of lesson 1
Browse files Browse the repository at this point in the history
  • Loading branch information
DakotaLMartinez committed Dec 12, 2022
1 parent 5a549ab commit 78cdedf
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions 01_Functions_&_Scope/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,71 @@ const inventory = [
*/

// Start here!
function helloWorld() {
let secret = "I love dates... The fruit ;)"
return "Hello, world!";
}

helloWorld()

function formatPrice(priceNum) {
return '$' + Number.parseFloat(priceNum).toFixed(2);
// return `$${Number.parseFloat(priceNum).toFixed(2)}`
}

console.log('formatPrice', formatPrice(inventory[0].price))

// πŸ’‘ Arrow functions vs regular functions

// βœ… create an arrow function version of the formatPrice function

// const formatPrice = (priceNum) => {
// return '$' + Number.parseFloat(priceNum).toFixed(2);
// // return `$${Number.parseFloat(priceNum).toFixed(2)}`
// }


// βœ… create a blurb() function that accepts a book as an argument and logs a message in the following format:
// 'Eloquent JavaScript: A Modern Introduction to Programming by Marjin Haverbeke is on sale for $10.00'

// books look like this:
// {
// id: 1,
// title: 'Eloquent JavaScript: A Modern Introduction to Programming',
// author: 'Marjin Haverbeke',
// price: 10.00,
// reviews: [{userID: 1, content:'Good book, but not great for new coders'}],
// inventory: 10,
// imageUrl: 'https://images-na.ssl-images-amazon.com/images/I/51IKycqTPUL._SX218_BO1,204,203,200_QL40_FMwebp_.jpg'
// },
function blurb(book) {
const title = book.title;
const author = book.author;
const price = formatPrice(book.price);
return `${title} by ${author} is on sale for ${price}`
}

console.log('blurb', blurb(inventory[0]))


// πŸ’‘ Difference between Block scope, Function scope, and Global scope

// βœ… create a variable `highestPricedBook`


let highestPriceBook;

// βœ… create a function `findHighestPricedBook` that finds that book and returns it
function findHighestPricedBook() {
highestPriceBook = inventory[0]; // set to first book
for (let i = 1; i < inventory.length; i++) {
if (highestPriceBook.price < inventory[i].price) {
highestPriceBook = inventory[i];
}
}
return highestPriceBook;
}

highestPriceBook = findHighestPricedBook()


// After Break
Expand All @@ -106,18 +148,32 @@ const inventory = [

// πŸ’‘ Practice using callbacks for iteration


// const nums = [1, 2, 3]
// nums.forEach(num => console.log(num*num))

// βœ… Create an array of the prices of all of the books

let prices = [];
inventory.forEach(book => prices.push(formatPrice(book.price)))

console.log('prices', prices);

// βœ… Create an array of simplified book objects

const simplified = inventory.map(book => {
return {
title: book.title,
author: book.author,
price: book.price
}
})


// βœ… Create an array of strings from the inventory in the following format:
// 'Eloquent JavaScript: A Modern Introduction to Programming by Marjin Haverbeke is on sale for $10.00'

// const blurbs = inventory.map(book => blurb(book))
const blurbs = inventory.map(blurb)

console.log('blurbs', blurbs);
// πŸ’‘ When do I use forEach vs map?

0 comments on commit 78cdedf

Please sign in to comment.