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 4615066
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:47:13+00:00 2026-05-22T01:47:13+00:00

I’ve already implemented this, but as I’m not familiar with the FOR loop statement

  • 0

I’ve already implemented this, but as I’m not familiar with the FOR loop statement I’ve done it this way as shown below!
Can you please help me shorten this code using the FOR loop statement and explain me how this works! Thanks

Here is my code:

var inputs = new Array();
inputs["a"] = document.getElementById("a").value;
inputs["b"] = document.getElementById("b").value;
inputs["c"] = document.getElementById("c").value;
inputs["d"] = document.getElementById("d").value;
inputs["e"] = document.getElementById("e").value;

And then I’ve added them up as:

var inputsvalue = inputs["a"] + inputs["b"] + inputs["c"] + inputs["d"] + inputs["e"];

Note that I’m adding the value of each input field and assigning them into a variable!

For example lets say the values for input fields a, b, c, d, e are as follows:

1, 2, 3, 4, 5

So I want them to be assigned to the variable “inputsvalue” like:

var inputsvalue = "12345";

That’s why I added them like that! So is there any other way to do this! In this example I only have 5 input fields, but what if there was about 100 input fields!

The purpose of this question is to learn how FOR loop statement works in this example!

Thanks in advance! 🙂

  • 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-05-22T01:47:13+00:00Added an answer on May 22, 2026 at 1:47 am
    // Too bad you can't use reduce.
    
    
    inputs.reduce(function(a,b){
        return a.concat(b);
    })
    
    // If you could use reduce, you could use map, as well.
    
    var sum= 'abcde'.split('').map(function(itm){
        return document.getElementById(itm).value;
    }).reduce(function(a, b){
        return a.concat(b);
    });
    alert(sum)
    
    /*    New browsers include map and reduce-
        you can add them to older browsers,
        but it might make your for in loops break,
        if you use for in loops to iterate arrays.
    */
    
    if(!Array.prototype.map){
        Array.prototype.map= function(fun, scope){
            var T= this, L= T.length, A= Array(L), i= 0;
            if(typeof fun== 'function'){
                while(i< L){
                    if(i in T){
                        A[i]= fun.call(scope, T[i], i, T);
                    }
                    ++i;
                }
                return A;
            }
        }
    }
    if(!Array.prototype.reduce){
        Array.prototype.reduce= function(fun, temp, scope){
            var T= this, i= 0, len= T.length, temp;
            if(typeof fun=== 'function'){
                if(temp== undefined) temp= T[i++];
                while(i < len){
                    if(i in T) temp= fun.call(scope, temp, T[i], i, T);
                    i++;
                }
            }
            return temp;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.