Programming Homework Help. javascript
JavaScript foundations: Object shorthand & spread
Instructions
To complete this Practice problem, you will update the existing functions in src/solution.js
to use new syntax you have learned. Use object shorthand and the spread operator whenever possible.
Note: The second function makes use of the .concat()
method. While you will be replacing it, you can learn more about it at the link below.
/*
Modify each function below to continue working with the suggested syntax.
When a function’s parameters reference `product`, it is referring to an object. Each object has the following shape:
{
name: “Washed Black Denim Overalls”,
priceInCents: 12000,
availableSizes: [ 28, 30, 32, 36 ]
}
*/
// `salesTax` is a decimal number, like 0.065
function createSalesProduct(product, salesTax) {
return {
name: product.name,
availableSizes: product.availableSizes,
salesTax: 0.065,
priceInCents: product.priceInCents * (1 + salesTax),
};
}
function joinSizes(productA, productB) {
const result = …productA.availableSizes, …productB.availableSizes;
}
return result;
}
JavaScript foundations: Default parameters
Instructions
To complete this Practice problem, you will do the following:
- Update each function to use argument defaults. You will also need to destructure objects.
getPriceInDollars()
You will need to update the function so that it works in the following ways:
// Typical use
getPriceInDollars({
name: "Slip Dress",
priceInCents: 8800,
availableSizes: [0, 2, 4, 6, 10, 12, 16],
}); //> $88.00
// Missing `priceInCents` key
getPriceInDollars({
name: "Slip Dress",
availableSizes: [0, 2, 4, 6, 10, 12, 16],
}); //> $0.00
// No arguments
getPriceInDollars(); //> $0.00
checkIfSizeIsAvailable()
You will need to update the function so that it works in the following ways:
// Typical use
checkIfSizeIsAvailable(
{
name: "Slip Dress",
priceInCents: 8800,
availableSizes: [0, 2, 4, 6, 10, 12, 16],
},
10
); //> true
// Missing `availableSizes` key
checkIfSizeIsAvailable(
{
name: "Slip Dress",
priceInCents: 8800,
},
10
); //> false
// Missing entire product
checkIfSizeIsAvailable(undefined, 10); //> false
// No arguments
checkIfSizeIsAvailable(); //> false
/*
The following functions have various syntax errors in them. Fix the bugs to get the tests to pass!
When any of the following function’s parameters reference `product`, they are referencing an object with the following shape:
{
name: “Slip Dress”,
priceInCents: 8800,
availableSizes: [ 0, 2, 4, 6, 10, 12, 16 ]
}
*/
function getPriceInDollars(product) {
return “$” + (product.priceInCents / 100).toFixed(2);
}
// `size` is a number between 0 and 16.
function checkIfSizeIsAvailable(product, size) {
let sizes = product.availableSizes;
for (let i = 0; i < sizes.length; i++) {
if (sizes[i] === size) {
return true;
}
}
return false;
}