Modifying a value within a loop

var currentmix = document.getElementById('custommix').value.split(",");
        var additionalcost = 0;

        for (i = 0; i < currentmix.length; i++) {
            if (currentmix[i] != "") {
                var mixinpriceid = "mixinprice"+currentmix[i];
                additionalcost += parseFloat(document.getElementById(mixinpriceid).value));
            }
        }

This snippet seems to have a bug :(

Despite each iteration providing the correct number through

parseFloat(document.getElementById(mixinpriceid).value)
, it does not seem to impact the variable "additionalcost" beyond the loop.

Any suggestions on how to address this issue?

Answer №1

Can you please review this statement as there appears to be an extra parentheses included

additionalcost += parseFloat(document.getElementById(mixinpriceid).value)); 

Answer №2

It appears that there is an extra parenthesis in Sathish's response. The following code has been modified to work without it.

var currentmix = "12345";
var additionalprice = 0;


for (i = 0; i < currentmix.length; i++) {
  if (currentmix[i] != "") {
    var mixinpriceid = "mixinprice" + currentmix[i];
    additionalprice += parseFloat(document.getElementById(mixinpriceid).value);

    console.log(additionalprice);
  }
}

console.log("Final Value: " + additionalprice);
<input id="mixinprice1" value="100">mixinprice1</input>
<input id="mixinprice2" value="200">mixinprice2</input>
<input id="mixinprice3" value="300">mixinprice3</input>
<input id="mixinprice4" value="400">mixinprice4</input>
<input id="mixinprice5" value="500">mixinprice5</input>

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

Whenever I try to run ionic serve, an error pops up saying: "typescript: node_modules/@types/node/worker_threads.d.ts, line: 8 '=' expected."

My Ionic Info: cli packages: (/usr/local/lib/node_modules) @ionic/cli-utils : 1.9.0 ionic (Ionic CLI) : 3.9.0 Global Packages: Cordova CLI : 9.0.0 (<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8f839e88839a8dc180858eacd5c2 ...

Exploring the connections among nodes in a Force-Directed Graph using D3.js JavaScript

I am currently working on a complex graph that consists of around 150 nodes using D3 Javascript. The concept behind it is quite intriguing: Each node is interconnected with 100 other nodes. Out of these 100 nodes, 49 are further linked to another set of ...

Creating PHP functions that return a JSON string when invoked - a simple guide

I have created a script that contains various functionalities, including fetching data from a database and encoding it into JSON. However, I want to be able to call different functions to execute these scripts separately. When I attempted to define and c ...

Distributing actions within stores with namespaces (Vuex/Nuxt)

I'm encountering an issue with accessing actions in namespaced stores within a Nuxt SPA. For example, let's consider a store file named "example.js" located in the store directory: import Vuex from "vuex"; const createStore = ...

Javascript - Accessing a specific element in an array using a variable

I am currently developing a webpage that interacts with a CGI backend. While the CGI backend is functioning well, my limited knowledge of JavaScript is making it hard for me to manage the results retrieved from AJAX JSON requests. Here's what I have: ...

Having issues with AngularJS where ng-table/ng-repeat rows are failing to load properly

I've been following a tutorial on using ng-Table, which can be found Here. When I download the example from Git, it loads without any issues. However, when I try to replicate even the simplest example myself, the column headers and filters load but th ...

I encounter a CORS issue on Heroku, but strangely it only occurs after 20 minutes of deploying. Prior to that time frame, everything functions seamlessly

Today, I encountered a strange issue while hosting my first Node JS backend on Heroku. Everything runs smoothly when I register/login immediately after deploying the backend. However, if I try to register/login after about 15 minutes, I start receiving a C ...

Having trouble passing multiple arrays as parameters into setTimeout to create a slideshow

I've been working on a project to create a basic slideshow with 3 rotating images that should restart as soon as the last one is displayed, with a 5000ms interval between each image. <div id="slideshow">&nbsp;</div> <script type= ...

Utilize a Chrome Content Script to intercept jQuery delegated event handlers and take control

After developing a Chrome extension that intercepts form submissions in specific circumstances, I encountered an issue with a particular website utilizing jQuery's delegate function. My extension is built with raw JavaScript, excluding jQuery to prev ...

Move the menu button to the right of the title slide, beyond the edge of the card

Having an issue with the MuiCardHeader component. <CardHeader disableTypography avatar={renderAvatar()} action={ <IconButton onClick={toggleMenu}> <img src={MoreIcon} alt=""/> </IconButton ...

Activate the class only on the current element

I need help with a specific functionality on my website. I have multiple sections with content and buttons, and when a button is clicked, I want to add an active class to the corresponding content section within the same row. <div id="1" class="row"> ...

The Mystery of jQuery Isotope Plugin: Why Can't I Add "display:none" Inline?

I'm currently in the process of integrating Isotope into my latest Wordpress theme. However, I've encountered an issue where it's not appearing on the page due to some external factor adding an inline style (display:none) to the main isotope ...

Launching a call to action through Ajax in Zend Framework triggers a redirect, but subsequent calls prove to be effective

I've encountered an intriguing issue that I'm hoping someone else has come across and resolved before. In this particular application, I have implemented a 'change my password' option for users. If a user forgets their password, they c ...

Incorporating a Script into Your NextJS Project using Typescript

I've been trying to insert a script from GameChanger () and they provided me with this code: <!-- Place this div wherever you want the widget to be displayed --> <div id="gc-scoreboard-widget-umpl"></div> <!-- Insert th ...

Dealing with 404 Errors in AngularJS using ui-router's resolve feature

As a newcomer to AngularJS and ui-router, I am currently facing the challenge of handling 404 errors for resources not found. My goal is to display an error message without changing the URL in the address bar. Here is how I have configured my states: app ...

Steps for removing an embed after a set time period

I've been using message.delete({timeout: 3000}) to remove messages with the prefix. Now I'm wondering how I can also delete the embed I sent after a certain amount of time. if (!args[0]) return message.channel.send({ embed: { color: 1677720 ...

Gathering all the indicated whole numbers from a string and placing them in an array

Upon decoding the URL query using decodeURIComponent and splitting it, the resulting array looks like this: ["pcs_availability:Online", "price:[1500 TO 1999.99]"]. I am trying to extract the proper integers from this array as shown in [1999.99]. In some ca ...

How can I transfer checkbox data to another page utilizing ajax or javascript?

These are the checkboxes I am using and I want to pass the selected items' values to another page using ajax with javascript. I am looking to send the selected checkbox values through ajax to a different page... Your help would be greatly appreciate ...

I can't seem to figure out why I continue to receive the error message stating that 'app.get is not a function'

Below is the code I am currently using: const request = require('request'); const app = require('express'); app.get('/', function (req, res) { res.send('hello world'); }); app.listen(3000); Unfortunately, I keep e ...

What is the best way to locate the index of a td element that contains the text "Up" using jQuery, and then utilize that index to choose the corresponding

I have a table that has the following structure: <table> <tr><td>a</td><td>b</td><td>up</td><td>d</td></tr> <tr><td>0</td><td>1</td><td>99</td>< ...