THINKFUL TAKE HOME

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Create a function named cartPrice to find the total cost of all the items in the shopping cart. The function should accept an array of items as its argument return the total cost of the items, as a number As shown in the example shopping cart data below, the shopping cart is an array of items. Each item is an object with keys itemName, type, and price. Your function cartPrice should add together the price of each item in the cart, and return that number. For example, passing in the sample array items, your function should return 328.98.

//accept array if items as arg //return total cost of items as a number function cartPrice(items){ let item = 0; for(let i = 0; i < items.length; i++){ item += items[i].price } return item }

Create a function named mostExpensiveItemName to find the name of the highest priced item in the shopping cart . The function should accept an array of items as its argument return the name of the most expensive item, as a string As shown in the example shopping cart data below, the shopping cart is an array of items. Each item is an object with keys itemName, type, and price. Your function mostExpensiveItemName should find the highest priced item in the cart, and return its itemName. For example, passing in the sample array items, your function should return "Creation 3005". mostExpensiveItemName(items) //=> "Creation 3005" Note: if there are no items in the array passed in, your mostExpensiveItemName should return undefined. Example Shop

//array of items as its arg //return name of most expensive item as string function mostExpensiveItemName(items){ if(items.length === 0){ return undefined } let item = 0 for(let i = 0; i < items.length; i++){ if(items[i].price > items[item].price){ item = i; } } return items[item].itemName; }

Create a function called priceFilter that returns all the items whose price is under a certain amount. The function should take in two arguments, an array of items and an amount (a number) return an array of items that have a price below the amount. As shown in the example shopping cart data below, the shopping cart is an array of items. Each item is an object with keys itemName, type, and price. Notes If there are no items have a price below the amount, your priceFilter function should return an empty array [].

//create function pricefilter //returns all items whose price is under certain amount //takes two arguments, array of items and an amount //if no item available at input amount return empty array function priceFilter(items, amount){ let item = []; for(let i = 0; i < items.length; i++){ if(items[i].price < amount){ item.push(items[i]) } } return item }

Write a function named countCorrectAnswers that takes in an array of student answers returns the number of answers that have isCorrect set to true Using the example answers below, countCorrectAnswers(answers); //=> 3 If none of the responses are correct, return 0.

//write function countCorrectAnswers //takes array of student answrs //returns number of answers that have isCorrect set to true //if none are correct return 0 function countCorrectAnswers(studentAnswers){ let score = 0; for(let i = 0; i < studentAnswers.length; i++){ if(studentAnswers[i].isCorrect === true){ score++; } } return score }

Write a function named findAnswer that takes in two arguments: an array of student answers, and a string for the question to match returns the item in the array that matches that question Notes: If none of the student's answers match the question, return undefined If more than one of the students' answers match the question, return the first one in the array

//write function findAnswer //takes two args array of studentAnswers & a string for question to match //returns the item in the array that matches question //if no answer matches question return undefined //if more than one of the answers match question return first one in array const findAnswer = (answers, question) => answers.find( answer => answer.question === question );

Write a function named filterAnswersByType that takes in an array of student answers returns an array of the answers that have isEssayQuestion set to true If none of the answers are essay questions, return an empty array.

//write function named filterAnswersByType //takes in array of student answers //returns array of answers that have isEssayQuestion set to true function filterAnswersByType(studentAnswer){ let qType = []; for(let i = 0; i < studentAnswer.length; i++){ if(studentAnswer[i].isEssayQuestion === true){ qType.push(studentAnswer[i]); } } return qType; }

Write a function named checkForPlagiarism that takes two arguments: an array of student answers, and snippet of text to check returns true if any of the essay question responses contain the text, and returns false otherwise For each essay question in the answers array, check whether the response contains the snippet of text. If it does, return true. Notes: Only essay question responses count as plagiarism. If an answer to a non-essay question contains the snippet of text, that doesn't mean you should necessarily return true.

function checkForPlagiarism(answer, snippet){ for(let i = 0; i <answer.length; i++){ if(answer[i].isEssayQuestion === true){ if(answer[i].response.includes(snippet)){ return true } } } return false }

Create a function called priceLookup to find the price of a single item. The function should take in two arguments: an array of items and an item name as a string return the price (as a number) of the item with that name For example, using the sample items array below: priceLookup(items, "Effective Programming Habits") //=> 13.99 As shown in the example shopping cart data below, the shopping cart is an array of items. Each item is an object with keys itemName, type, and price. Notes If there are no items that match the name passed in, your priceLookup function should return undefined. If there is more than one item in the array that matches the name, your function priceLookup should return the price of the first one that matches.

function priceLookup(items, name) { let result = 0; for ( let i = 0; i < items.length; i++) { if (name === items[i].itemName) { // the return will break the loop and exit the function return items[i].price; } } if (result === 0) { return undefined; } return result; }


Set pelajaran terkait

Hand-On Machine Learning with Scikit-Learn, Keras, & TensorFlow (Terminologies)

View Set

BASIC VEHICLE TECHNOLOGIES 1: COMFORT

View Set

BIO EXAM FINAL multiple choices from previous exams + chapters 23/24/25 study guide questions

View Set

Princeton Review Management of Care Drill 4

View Set

Psalm 110 - Flashcard MC questions - Ted Hildebrandt

View Set

Men and Women reproductive system cancers (IGGY ONLY)

View Set

Thigh: tensor fasscia latae, fascia latae, and iliotibial band

View Set