Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9256195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:53:02+00:00 2026-06-18T11:53:02+00:00

I have some values that come randomized to an variable. Now i want to

  • 0

I have some values that come randomized to an variable.
Now i want to add every value to each other to get an total sum but the loop wont add them together.

The index variable work when i console log it. but not the total sum of a current value and the index value.
Any tips ?

This is the loop
// The indexValue is values I get from an array//

  var indexValue = index;  // Random number from 1 - 10
    var totalSum = 0;

   for(var x = 0; x <indexValue; x++){

   var currentValue = index[x];
   totalSum += currentValue;
    }

    console.log(totalSum);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:53:04+00:00Added an answer on June 18, 2026 at 11:53 am

    I’m assuming since you’re referencing index[x] that index is an array. If so, you are assigning the wrong value to indexValue. Try this:

    var indexValue = index.length;
    

    What this does is assign the length of the array to the variable indexValue. Now the for loop will run from 0 to n where n is the length of the array.

    This should get you the values you need.

    Hope this helps.

    EDIT:

    Below is a link to a jsFiddle I created with your example and the code explained:

    jsFiddle

    var index = [1,2,3,4,5];  
    
    var indexValue = index.length;  // It's imperative that this is an array
    var totalSum = 0;
    
    for(var x = 0; x <indexValue; x++){
        totalSum += index[x];
    }
    
    alert(totalSum);
    

    The code above is a modified version of what you posted. The current value is not needed and has been removed. In this case, I created an array and stored the values in index. The length is computed, and the values are added together.

    For the easy sake of testing, I changed console.log(totalSum); to alert(totalSum);. It will produce the same value just the same.

    Just a reference note on console.log. Some browsers (mainly IE) who do not attach the debugger to the process automatically without a page refresh will through an error as console will be undefined. Here is a quick fix for that:

    if(window.console && window.console.log) {
        console.log(totalSum);
    } 
    

    This checks that (a) the console object is attached to the global window object and (b) that the log object of console is attached to the console object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Introduction I have some sort of values that I might want to access several
If I have a class with some value member that I want to store
In my C++ application, I have some values that act as codes to represent
I have some code that sets up a dictionary with some defualt values for
I have some code that produces undefined values in an array making no sense.
I have some code here that uses bitsets to store many 1 bit values
I have php code that puts some values into an Array as follows: $hunter=addslashes($MessageArray[1]);
We have our own data streaming algorithm that include some metadata+records+fields values. Currently we
I have the problem, that MSSQL Server 2000 should select some distinct values from
I have some code that first selects an option value in a dropdown menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.