From 17cb97ad141c0890e4326aee865eaef1bcf33004 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 17 Nov 2021 17:35:10 -0600 Subject: [PATCH] backing up project code --- flowers.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 flowers.js create mode 100644 test.js diff --git a/flowers.js b/flowers.js new file mode 100644 index 0000000..facd8bb --- /dev/null +++ b/flowers.js @@ -0,0 +1,68 @@ +const flowers = [ + { + id: 1, + color: "white", + species: "White Rose", + price: 0.9 + }, + { + id: 2, + color: "red", + species: "Red Tulip", + price: 1.10 + } + +] + +const addFlower = (flowerObject) => { + const lastIndex = flowers.length - 1 + const currentLastFlower = flowers[lastIndex] + const maxId = currentLastFlower.id + const idForNewToy = maxId + 1 + flowerObject.id = idForNewToy + /* + This function should add an `id` property + to the object that was sent to the function. + The value of the `id` property should be 1 + greater than the current maximum `id` in the + array. + + Once the `id` property has been added to the, + object, the object should then be put into the + `flowers` array. + */ +// This differs from my Leonid's toys project with Monica to the best of my understanding because I am starting with object already in the array instead of using a factory function to create and push objects into the array + +flowers.push(flowerObject) +} + +console.log(flowers) + +const findExpensiveFlowers = () => { + const expensiveFlowers = [] // Do not change this code + + /* + Write a for..of loop that iterate the array + of flowers, and if the price of a flower is + greater than or equal to 1.00, it should be + added to the `expensiveFlowers` array. + */ + + for(const flower of flowers){ + if (flower.price >= 1) { + expensiveFlowers.push(flower) + } + } + + + + return expensiveFlowers // Do not change this code +} + + + +// Do not touch this code +module.exports = { + findExpensiveFlowers, addFlower +} + diff --git a/test.js b/test.js new file mode 100644 index 0000000..dd9470d --- /dev/null +++ b/test.js @@ -0,0 +1,65 @@ +const { addFlower, findExpensiveFlowers } = require("./flowers.js") + +addFlower({ + color: "Orange", + species: "Tulip", + price: 0.95 +}) + +addFlower({ + color: "White", + species: "Baby Breath", + price: 0.40 +}) + +addFlower({ + color: "Blue", + species: "Orchid", + price: 1.05 +}) + +const expensiveFlowers = findExpensiveFlowers() + +console.log(` + + + + + + + +Test: Three new flowers added to array. +Price #1 is 0.95 +Price #2 is 0.40 +Price #3 is 1.05 +`) + +if (expensiveFlowers.length !== 2 || Array.isArray(expensiveFlowers[0])) { + console.log(` +************************* +**** TEST FAILED **** +************************* + +The test code expected that two flowers should be +in the expensiveFlowers array, and that each item +in the array is an object. + +Make sure that you have an \`if\` condition that +is checking the price property of each flower, and +that only flowers whose price is >= 1.00 are being +added. +`) +} else { + console.log(` +**************************** +**** TEST SUCCEEDED **** +**************************** + +Congratulations on writing valid code for your first +self-assessment project. +`) +} + +console.log(expensiveFlowers) + +