Menu
CoddyTech

What To Buy

Part of the Fundamentals section of Coddy's JavaScript journey. Lesson 77 of 77.

challenge icon

Challenge

Medium

Write a function named analyzeBudget that:

  1. Takes three arguments: a list of prices, a list of item names, and a budget per item.
  2. Prints the following information exactly as shown below:
    • Affordable items: [items]
    • Total budget needed: [total]
    • Items out of budget: [count]

Note: Each item name includes its own quote marks, and every name after the first starts with a space (for example ' "Pen"'). Print the affordable items exactly as they are, without trimming, separated by a comma with no added space: the space you see after each comma is the item's own leading space. An item keeps that space even when it is the first affordable one, so Affordable items: can be followed by two spaces.

Example:
For prices = [10, 20, 5, 15], items = ['"Notebook"', ' "Pen"', ' "Eraser"', ' "Bag"'], and budget = 10, the output should be:

Affordable items: "Notebook", "Eraser"
Total budget needed: 15
Items out of budget: 2

Try it yourself

function analyzeBudget(prices, items, budget) {
    // Write code here
}

All lessons in Fundamentals

Practice on your own: Online JavaScript compiler