Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
create a function findItems

I have been learning Javascript for a few weeks now(mostly just arrays, functions, for loops and if statements). I have come across a problem that has stumped me and would greatly appreciate any kind of insight or breakdown of the problem.

Here is the problem:

Create a function findItems that
•   takes in two arguments: an array of items, and a type of item as a string
•   returns an array of items that have a type that matches the type passed in

Using the example data below, findItems(items, "book") /* =>

[{ 
    itemName: "Effective Programming Habits", 
    type: "book", 
    price: 18.99
}]
  */

Note: after you write this function and the first set of tests pass, a second set of tests will show up for part 2. Part 2 - Find Items, Extended

Add the following features to your findItems function:
    •   If there are no items in the cart array, return the string "Your cart does not have any items in it."
    •   If there are no items that match the given type, return the string "No items found of that type. Please search for a different item.".
Sample Shopping Cart Data

let items = [
  { 
    itemName: "Effective Programming Habits", 
    type: "book", 
    price: 18.99
  },
  {
    itemName: "Creation 3005", 
    type: "computer",
    price: 399.99
  },
  {
    itemName: "Orangebook Pro", 
    type: "computer",
    price: 899.99
  }
];

Would anyone know how to solve this? Or be able to offer a breakdown of it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should loop over the given array, then add any objects with the type field to a temporary array, then return the array. Something like this:

function findItems(arr, type) {
    let temp = [];
    for (let i = 0; i < arr.length; i++) {
        const item = arr[i];
        if (item.type === type) {
            temp.push(item);
        }
    }
    return temp;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!