/* INSTRUCTIONS
To run this file, click in the Console below and type: node 1_object.js
If the console isn't visible, click the View menu above and choose Show Console.
You can clear the console by typing `clear` and pressing enter.
If your program is stuck in an infinite loop, you can break out of the program by typing ctrl + C.
*/
// 1. Create an object named book using object literal syntax
// 2. Add a title property to the book object and assign it a string value
// 3. Add an author property to the book object and assign it a string value
// 4. Add a publish_year to the book object and assign it a number to represent the year the book was published
/* 5. Use a for-in loop to print the book object to the console so the final output looks something like this:
title: Harry Potter and the Sorcerer's Stone
author: J.K. Rowling
publish_year: 1997
*/
// 6. Run your code by typing node 1_object.js in the console below
let book = {
title: "Harry Potter and the Sorcerer's Stone",
author: 'J.K. Rowling',
publish_year: 1997
};
for (let book_property in book) {
console.log(book_property + ': ' + book[book_property]);
}
/* INSTRUCTIONS
To run this file, click in the Console below and type: node 2_product.js
If the console isn't visible, click the View menu above and choose Show Console.
You can clear the console by typing `clear` and pressing enter.
If your program is stuck in an infinite loop, you can break out of the program by typing ctrl + C.
*/
// 1. Create an object named product, with the following 3 properties.
// Make up the values for the properties, just make sure the inventory is a whole number (it's the number of that product currently in stock) and unit_price is a floating point number like 45.99
// -- name
// -- inventory
// -- unit_price
// before moving to the next step, make sure you've created the object correctly by logging it to the console like this: console.log(product)
// 2. Create a function named addInventory(). The function should accept 2 parameters -- the product object, and the number to add to the inventory. The function adjusts the product object's inventory property by adding the number passed into the function.
// For example, if the inventory of the product is currently 2, calling addInventory(product, 3) will set the value of inventory to 5
/* Add a console.log() message to the function that includes a message that looks something like this:
"3 chairs added to the inventory"
In this example, "chair" is the name of the product.
*/
// 3. Call the addInventory() function
// 4. Create a function named processSale(). The function should accept 2 parameters -- the product object, and the number to of products that were sold. The function adjusts the product object's inventory property by subtracting the number passed into the function. The function also returns the total sale which is the unit_price multiplied by the number passed into the function
// For example, if the inventory of the product is currently 5 and the unit_price is 10, calling processSale(product, 2) will set the value of inventory to 3 and return the value of 20.
/* Add a console.log() message to the function that includes a message that looks something like this:
"2 chairs sold"
In this example, "chair" is the name of the product.
*/
/* 5. Call the processSale() function. When you call the processSale() function log the return value to the console so the message looks something like this:
"Total sale: $20"
*/
// 6. Run your code by typing node 2_product.js in the console below
let product = {
name: 'Bud',
inventory: 15,
unit_price: 3.40
};
function addInventory(product, add_number) {
product.inventory += add_number;
console.log(add_number + " " + product['name'] + ' added to the inventory. ' + product['inventory'] + ' in stock')
}
function processSale(product, sold_items) {
product.inventory -= sold_items;
console.log(sold_items + ' ' + product.name + ' sold!');
console.log('Total sale: $' + (product.unit_price * sold_items));
}
addInventory(product, 5)
processSale(product, 7)
/* INSTRUCTIONS
To run this file, click in the Console below and type: node 3_final_products.js
If the console isn't visible, click the View menu above and choose Show Console.
You can clear the console by typing `clear` and pressing enter.
If your program is stuck in an infinite loop, you can break out of the program by typing ctrl + C.
*/
// 1. Create an array named products.
// 2. Add objects to the array. Each object should be a single product, with the following 4 properties.
// Make up the values for the properties, just make sure the inventory is a whole number (it's the number of that product currently in stock) and unit_price is a floating point number like 45.99
// -- name
// -- inventory
// -- unit_price
// 3. Create a function named listProducts(). The function should accept 1 parameter -- the array of products. It should return an array of just the names of the products.
// 4. Call the listProducts() function and log the returned value to the console.
// 5. Create a function names totalValue(). The function should accept 1 parameter -- the array of products. It should return the total value of all of the products in the array. You calculate the value of one product by multiplying the inventory value by the unit_price value
// 6. Call the totalValue() function and log the returned value to the console.
// 7. Run your code by typing node 3_final_products.js in the console below
products = []
products.push(
{
name: 'Coca',
inventory: 18,
unit_price: 2.50
},
{
name: 'Guarana',
inventory: 13,
unit_price: 2.25
},
{
name: 'Fanta',
inventory: 9,
unit_price: 1.99
},
{
name: 'Agua',
inventory: 25,
unit_price: .99
}
);
function listProducts(products) {
let name_products = [];
products.forEach(element => {
name_products.push(element['name']);
});
return name_products;
}
function totalValue(products) {
let total = 0;
products.forEach(element => {
total += element['unit_price'];
});
return total;
}
console.log(listProducts(products));
console.log(totalValue(products))