Looping inside a function with a for loop

Hey there, I'm currently facing an issue with a piece of JavaScript code. I'm trying to create a function that will notify me whether or not a specific item is in the basket.

const amazonBasket = {
  glasses: 1,
  books: 2,
  floss: 100,
};

function checkBasket(basket, lookingFor) {
  for (item in basket) {
    if (item === lookingFor) {
      return `${lookingFor} is in your basket`;
    }
  }
  return `That item is not in your basket`;

}

checkBasket(amazonBasket, camera);

Answer №1

To properly use the function, you should call it like this

checkItems(amazonBasket, "camera");
, where amazonBasket is an object and "camera" is the specific item key you are searching for.

An improved and cleaner solution would be

function checkItems(basket, item) {
  return basket[item] ? `${item} is in your basket` : `That item is not in your basket`;
}

// Correct way of calling the function
const amazonBasket = { glasses: 1,books: 2, floss: 100};
checkItems(amazonBasket, "camera");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When using Webdriver to upload a file, the sendKeys() method does not trigger any action

I'm currently working on automating the file upload process using Selenium Webdriver. After researching numerous questions on stackoverflow and implementing the suggestions provided, I was able to successfully execute the code on a test page: Howeve ...

Eliminate duplicate items using the reduce method in JavaScript

Working with a set of Json Objects, I use a javascript map function to list each field along with an array of its possible types. For example: birthDate, [Date, String, String, String, String] isMarried, [Boolean, Boolean, Boolean, Boolean, String] name, ...

Using ESLint to enforce snake_case naming conventions within TypeScript Type properties

When working with TypeScript, I prefer to use snake_case for properties within my Interfaces or Types. To enforce this rule, I have configured the ESLint rule camelcase as follows: 'camelcase': ["error", {properties: "never"}], Even though the E ...

Vuejs - The router is not rendering the third component in the app, even though the first two components are functioning properly

After creating my very first Vue app, I encountered an issue with the router. While the first two components function perfectly, the third one does not seem to work as expected. Below is a snippet of my code: index.js import Vue from 'vue' impo ...

The content of XMLHttpRequest is accessible via the property response

Typically, when we want to retrieve data using AJAX, we would use code like this: var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ elem.innerHTML = xhr.responseText; ...

The function of the "enter/next" key on mobile browsers when inputting into a numeric field

I am working on a piece of code that only allows number inputs in a specific box. Previously, I used type="text", but this resulted in the full keyboard appearing on mobile browsers by default. To solve this issue, I switched to type="number". While mobile ...

Adding a user mention to a message in a reactjs application - a step-by-step guide

I am a beginner in JavaScript programming and I want to incorporate user mentions into messages using React without relying on any plugins. What is my goal? I have an input field for messages. When a user types the @ symbol, I want a dropdown menu to app ...

The Click Event is failing to trigger for a dynamically created ID within a span element

I am currently working on a Task Project. In this project, I have been adding items dynamically and generating items dynamically as well. However, when it comes to deleting items by clicking on a span element, the click event doesn't seem to work. I ...

Retrieving JSON values on the fly

{ "id":["123"], "optionid_123":"98" } I am having trouble retrieving the value of optionid_* based on the variable id. I have attempted various methods, but none seem to be working as expected. The loop is contained within the appropriate function and ...

Trouble With Binding Input Field Inside Kendo Grid Using jQuery .on() Method

Lately, I have been focusing on a screen that incorporates a Grid (Kendo).... Within the Grid, there is a template that includes a straightforward button: <input type="button" class="iqs-review-btn btn btn-primary" value="REVIEW" /> I am attempting ...

Troubleshoot: JQuery unable to change background color in code

Is there a way to use JQuery to cycle through an array of colors and change the background color of a page whenever a specific button is clicked? I've tried implementing it with the following code, but it doesn't seem to work as expected. Here&a ...

Selenium's `getEval` function is unexpectedly returning null, despite the fact that the corresponding JavaScript

While using Selenium, I am trying to execute a getEval function with the following javascript: document.getElementById("j_id0:j_id3:mainBlock:j_id40").children[0].children[0].children[0].children[0].children[0].children[0] .children[2].children[0].childre ...

Unveil the Power of NodeJS FS Methods within Electron's Renderer Environment

I'm currently utilizing Quasar v2 for the development of my Electron application. However, in this updated version, native node modules no longer function on the renderer processes. To overcome this obstacle, there is a solution called electron-prel ...

Tips for implementing owl carousel in Nuxt.REACT_UNITS_CODIFY

Is there a way to make the script function on every page without the need for these pages to be reloaded? I have the Owl Carousel script in my static folder, and I have already included it in nuxt.config.js as shown below: head: { title: 'title&ap ...

Rotating arrows enhance the functionality of the accordion menu

I have successfully implemented a basic accordion with rotating arrows on click. Everything is working smoothly except for one issue: When I collapse one item and then try to collapse another, the previous arrow does not return to its default state. Is ...

Implement checkboxes to dynamically update radio buttons in HTML using AngularJS

My form consists of a checkbox and multiple radio buttons. The user should be able to use the checkbox to clear the selection of radio buttons or set a default value. If a radio button is selected, the checkbox should also be checked. The code provided me ...

Arranging a dictionary by its keys using Ramda

My task involves manipulating an array of items (specifically, rooms) in a program. I need to filter the array based on a certain property (rooms with more than 10 seats), group them by another property (the area the room is in), store them in a dictionary ...

Securing a WebSocket Server with Node.js: Step-by-Step Guide

My WebSocket server, running on node.js, is functional for ws:// but not for wss:// This server is operating on my Raspberry Pi B 3+. After changing ws:// to wss:// in the JavaScript file, it stopped functioning. The node.js server: const WebSocket = re ...

What are some creative ways to customize and animate the cursor or caret within an input field?

Just to clarify, I am working with React and Material-UI. I have a task at hand where I need to modify the appearance of the caret within an input element by adding an animation to it. I have managed to change its color using caret-color and set a default ...

Steps for generating a jQuery script to tally the checkboxes that have been ticked

[2]. In my quest to implement a checkbox functionality using jQuery, I have composed the following code snippet:- <!DOCTYPE html> <html lang="en"> <head> <title>Check Box using jQuery</title> </head> <bo ...