Can someone help me fix my JavaScript code in an external file? A simplified solution would be great!

Similar Question:
Issues with JavaScript Shopping Cart

This Javascript code seems to have some underlying issue preventing it from functioning correctly. It would be great if we could simplify it as well :)

var computer = new Array();

computer[0] = "10001, Nvidia Geforce GTX 690, 1200";
computer[1] = "10002, Raedon HD 7950, 450";
computer[2] = "20001, Ivy Bridge i7 3770, 400";
computer[3] = "20002, Ivy Bridge i7 3770k, 420";
...
...

</form>

Answer №1

Here are some points to consider in order to possibly resolve the issue:

  • Consider using parseFloat instead of ParseFloat,
  • Ensure that attributes are treated as strings, not integers,
  • Be cautious of using computer[x/2] which could result in a decimal array index,
  • Avoid repeatedly declaring variables within the for loop,
  • Try using the DOM instead of innerHTML.

Answer №2

(This is not the answer you're looking for.)

Let me propose a simplification, although there are a couple of things to keep in mind:

  1. The function could use a better name since it does more than just toggle checkboxes.
  2. You might want to consider using a JavaScript framework that streamlines DOM manipulation.
function check() {
    setChecks(false);
}

function uncheck() {
    setChecks(true);
}

function setChecks(checked) {
    var leftSide = document.getElementById('table_container_left');
    var inputs = leftSide.getElementsByTagName('input');
    var textValue = checked ? "1" : "0";
    var x;

    for (x = 0; x < inputs.length; x++)  {
        if (inputs[x].type == 'text')  {
            inputs[x].value = textValue;
        } else {
            inputs[x].checked = checked;
        }
    }
}

You may also want to think about initializing arrays immediately and using objects. Additionally, the object containing the computers should be named appropriately as computers. It's not just one computer, it's multiple computers; plural.

var computers = [
  { id: "10001", name: "Nvidia Geforce GTX 690", price: 1200 },
  { id: "10002", name: "Raedon HD 7950", price: 450 },
  // ...
];

Answer №3

When working with arrays, why not fully embrace their potential?

device[0] = [20001, "lorem ipsum dolor"];
device[1] = [20002, "ipsum lorem amet"];

Don't just scratch the surface - dive deep and make the most out of it! It's like buying a powerful tool and only using it for mundane tasks.

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

Generic partial application fails type checking when passing a varargs function argument

Here is a combinator I've developed that converts a function with multiple arguments into one that can be partially applied: type Tuple = any[]; const partial = <A extends Tuple, B extends Tuple, C> (f: (...args: (A & B)[]) => C, ...a ...

How to creatively position custom arrows in a React JS Nuka Carousel

Seeking assistance on properly positioning custom arrows within the Nuka Carousel component. After combining the decorators, I found that both of my arrows are appearing side by side. How can I address this issue? My goal is to have one arrow positioned in ...

`Why is it important to debug javascript code?`

I have some outdated JavaScript code that surprisingly still works in Internet Explorer from 2000-2002, but refuses to function properly in browsers like Firefox, Chrome, and Opera. I've come across various browser quirks where different browsers inte ...

The process of generating an efficient build gets stuck and never finishes

I am currently facing an issue while attempting to build my React app. Whenever I execute "npm run build," the terminal gets stuck at "Creating and optimized production build..." and doesn't complete the process. I have tried running this on a system ...

Tips for obtaining a reference to a subarray

I have a problem with referencing a sub array from an existing array. I need to update the original array when making modifications to the sub array. For example: byte[] array = {0 , 1, 2, 3, 4}; byte[] subarray = array.Skip(2).Take(3).ToArray(); subarray ...

Customizing the styling of buttons in Highcharts is disabled when in full screen mode

I've integrated highcharts into my Angular application and included a custom button inside the chart to navigate users to another page. However, I encountered an issue when trying to fullscreen the chart using the export menu. The position of the cus ...

The pure JavaScript function for changing the background color in CSS is not functioning properly

I'm having trouble understanding why the color does not change after clicking. Can anyone explain? function butt(color) { if(document.getElementById("bt").style.backgroundColor=="green"){ document.getElementById("bt").style.backgrou ...

Express 4: The requested route was not found by the router

Encountering a peculiar issue - the initial route functions properly, but when trying the parameterized route, a 404 error is returned. const express = require('express'); const router = express.Router(); router.route('/') .get(fu ...

What is the best way to alter the order of the .map function in JavaScript to display in ascending or descending order?

I currently have 25 songs in my Spotify playlist, with the possibility of more being added in the future. The songs are numbered from 1 to 25. However, the map() function I use only displays the top 10 songs (1 to 10). When a new song is added, it is assig ...

Stream data's modification to numerous controllers post ajax loading

One issue I encountered in my Angular app is related to the asynchronous loading of data from a server. Some controllers depend on this data, which is retrieved through an AJAX request. The problem arises because controllers end up pointing to an empty arr ...

Utilizing JSON API filtering within a Next.js application

Recently delving into the world of coding, I've embarked on a personal project that has presented me with a bit of a challenge regarding API filtering. My goal is to render data only if it contains a specific word, like "known_for_department==Directin ...

A beginner's guide to crafting a knex query with MySQL language

Within MySQL Workbench, I currently have the following code: USE my_db; SELECT transactions.created_at, price FROM transactions JOIN transactions_items ON transactions.id = transactions_items.transaction_id JOIN store_items ...

I am interested in designing a dynamic wave-themed background for a menu

I am looking to create a menu background that ripples like a wave when hovered over. Check out the first example here: https://i.sstatic.net/LoOWM.png I want to achieve the same effect. Can anyone help me with how I can do this? <ul> <li>likk ...

The Angular-Express combination is not being satisfied by the get request

`, I am a newcomer to the MEAN stack environment and I am struggling with fetching data from my Express server (which is set up with MongoDB) when a button is clicked. Even though the function associated with the button click is executed, the request does ...

Issue with API delivering zip file - Headers cannot be modified after being sent to the client

I've encountered a problem with my API that generates a zip file using the archiver module and sends it back as a response to be downloaded on the client side. Whenever I attempt to send the zip file back, I receive the error Error [ERR_HTTP_HEADERS_S ...

Having issues with the onclick _dopostback function in MVC? It seems like the post

When attempting to execute a postback in ASP.NET MVC after confirming with JavaScript, the following button is used: <input type="submit" id ="RemoveStatus" value="Remove Status" name="button" onclick="return CheckRemove();"/> The JavaScript code f ...

Is it possible to caution users before refreshing the page, while still allowing them to

I have a script that displays a warning message to the user when they attempt to refresh the page. It works perfectly for that purpose. However, my issue is that this same alert pops up when a user clicks on a button or a link within the website itself. I ...

Encountering a frustrating Npm error while trying to install a package, which persists in throwing

Encountering an error message while trying to run npm install npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\ node_modules\&bsol ...

Pressing the button to navigate to the top page does not function properly when using AngularJS Router link

Encountering an issue with AngularJS ng-include where I am trying to navigate to the top of a different page after clicking a button in the footer. HTML (index.html) <body> <a name="top"></a> <!-- header section --> <div ng- ...

Tips for incorporating dynamic colors into text containers in HTML: "Enhance your

I'm looking for a solution to change the color of a span every time a button is clicked. The colors are stored in an array like this: var colorArray = ["red","blue","red","red","green","blue","green","red"] The array values may vary as they are read ...