In JavaScript, the addition and subtraction buttons may become disabled after nested lists are used

Recently, I embarked on a project to enhance the functionality of restaurant table items and implement value-saving features.

Initially, everything worked smoothly with one item list. However, upon introducing a second list and attempting to manipulate items through buttons, the quantity would increase/decrease while the total sum remained unchanged.

While new items are accurately reflected in the sum, manipulating existing ones seems to be causing issues.

If you add the provided code snippet, you can run it yourself to observe this behavior.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Prototype</title>
    <style>
        // CSS styles here
    </style>


    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

</head>
<body onload="init()">
// HTML structure and script calls here

Thank you for your assistance!

// Embedded JavaScript functions and CSS styles

Answer №1

Once categories have been included, the 'inventory' array now contains 2 named properties:

[Softdrinks: Array(2), Juice: Array(3)]

Therefore, when searching for a price in checkPrice(), make sure to not include the list of products in the initial loop. Here's how you can do it:

function checkPrice(itemName) {
    let itemPrice = 0;

    for(let category in inventory){
      for(let product of inventory[category]){
          if(product.name === itemName){
              itemPrice = product.price;
              break;
          }
      }
    }

    return itemPrice;
}

Answer №2

Adjust the iteration of products in the findPrice() function.

The products variable contains a first-level object with nested lists of values. To access this information, you will need two nested for loops.

<!DOCTYPE html>
<html lang="en>
<head>
    <meta charset="UTF-8>
    & lt;title>Prototyp</title>
    <style>
        .table{
        text-align:center ;
        line-height:9vh ;
        height:10vh ;
        }
        
        .tablei{
        background-color:#e58e26 ;
        font-size:44pt ;
        }
        
        .
        ..
        ..

         
        

<!-- HTML content continues... -->

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

JavaScript accordions failing to open

I've encountered an issue with my website that includes JS accordions. Strangely, they are not opening on the live site, but they function properly on Codepen. I checked the console in Chrome and found no error messages, however, when I looked at the ...

Ways to structure this updateone query for mongoose formatting

UPDATE: After making adjustments to the query using arrayFilters recommended by someone here, the query is returning success. However, the values in the database are not being updated. I am attempting to update specific fields within a MongoDB collection ...

designing a modular socket.io framework in node.js

Suppose in a node.js application, we have an app.js file structured like this: var express = require('express') var app = express(); var server = http.createServer(app); ... module.exports = { app:app, server:server } Additionally, there ...

In main.js within the Google Maps API, an error is triggered when trying to setDraggable(false) on a

Here's a piece of JavaScript code I'm working with: google.maps.event.addListener(mark, 'dragend',x(mark)); function x(mark) { mark.setDraggable(false); } Recently, when I try to move a marker to a different position, I encounter ...

Is there a way to efficiently eliminate any unnecessary spaces, tabs, line breaks, and other unwanted characters from an input in order to sanitize a Python stdin input?

After numerous attempts with different variations of the code, I still have not been able to successfully clean up unstructured data entered by the user like this: "4231, 312 1231212,12 1 23 123,12,3,123 123 1,2, 3123" I am aiming to transform it ...

What could be causing this Ajax error I'm experiencing?

I am attempting to dynamically load pages on this website using JQuery and Ajax when a menu point is clicked. However, I am encountering errors during the loading process. What does the console message ""GET 'path to css file'" mean? (there ...

When implementing flex-grow 1 on a flex box, the remaining space is not being filled as expected

I am struggling to make the green div fill the remaining space of a flex box container. My goal is to have the top row (blue) adjust its height based on content, and then have the row below (green) take up the rest of the space. However, it seems like the ...

Obtain the month, day, or year from a timestamp

Can anyone provide guidance on extracting a specific date format (date, month, day, year) from a timestamp? I am interested in implementing this feature within a view using Backbone JS. ...

Adjusting the appearance of a JavaScript element based on its hierarchy level

Currently, I am utilizing Jqtree to create a drag and drop tree structure. My main goal is to customize the appearance of the tree based on different node levels. Javascript Tree Structure var data = [ { name: 'node1', id: 1, chi ...

Is it possible to use a Proxy-object instead of just an index when changing tabs in material-ui/Tabs?

Using material-ui tabs, I have a function component called OvertimesReport with Fixed Tabs and Full width tabs panel: const TabContainer = ({children, dir}) => ( <Typography component="div" dir={dir} style={{padding: 8 * 3}}> {children} & ...

What is the best way to trigger a jQuery UI dialog using an AJAX request?

My website includes a jQuery UI Dialog that opens a new window when I click the "create new user" button. Is there a way to open this window using an AJAX request? It would be useful if I could open the dialog-form from another page, such as dialog.html ...

When the cursor is placed over it, a new div is layered on top of an existing

Currently, I am working on a project with thumbnails that animate upon hovering using jQuery. One of the challenges I have encountered is adding a nested div inside the current div being hovered over. This nested div should have a background color with som ...

Tips for adjusting the content direction of a Popover

I am currently working on a component that includes the following code: import { Popover, PopoverTrigger, PopoverContent, PopoverBody, Portal, Button, } from "@chakra-ui/react"; import { ReactNode } from "react"; import { Co ...

Is there a way I can edit this text on the backend page?

Currently, I am attempting to implement a scrolling text box on a front-end page that will display the content from a text file named "msg.txt". <div class="scroll-slow"> <?php echo file_get_contents('../msg.txt'); ?> </div> ...

What is the best way to find the index of the smallest value in an array using JavaScript?

I recently created this function that currently outputs -1. function sayHello() { let buildingLevelsArray = [11,10,10]; var smallestIndex = buildingLevelsArray.indexOf(Math.max(buildingLevelsArray)); console.log(smallestIndex); } sayHello() ...

Can we leverage Angular service styles in scss?

I am working on a change-color.service.ts file that contains the following code snippet: public defaultStyles = { firstDesignBackgroundColor: '#a31329', firstDesignFontColor: '#ffffff', secondDesignBackgroundColor: '#d1 ...

Updating the UI in Angular for keyboard events is not working as expected

Looking to update the page based on keyboard events. I've connected the keyboard event using $window.document.onkeydown. (I understand it's not ideal, but it should still work) However, despite changing the model, I'm not seeing any updates ...

Utilize ReactJS and AJAX to easily upload images

Having trouble with uploading an image through a form to a url/api. When I submit the form, I'm calling the handleImgUpload() method. The issue is that the request being sent is coming back empty. Seems like there might be a problem with new FormData ...

applying conditional rendering in vue.js

I'm currently working on developing a chat application using the guidelines outlined in this tutorial: https://socket.io/get-started/private-messaging-part-1/ One of my goals is to customize the appearance of the messages, so that when a message is s ...

What is the process for altering fonts in a Chrome OS application?

After extensive research on the Internet, I have been unable to find clear instructions on how to modify the text font in a Chrome OS application. Most search results pertain to adjusting the font for the OS user rather than the developer of the app. My i ...