-1

Goal: Gather all the results from a loop (that loops through JSON object array) and add those values together to produce the overall number of credits for a user. Every time they take a course, they receive credit, but right now, I need to figure out how to add them together. I am new to Javascript and am trying to figure out how to create a function that continually updates (via a for loop), so that I can produce it via HTML in the web page.

I was looking at this stack example that loops together values (for a game) and tried to mimic it. I was also looking at the for loop example via this JS tutorial.

var sum = 0; for (var i = 1; i <= 50; i++) {    sum = sum + i; }
alert("Sum = " + sum);    // => Sum = 1275

It consists of three parts, separated by semicolons. The first is the initializer (var i = 1) which initializes the loop and is executed only once at the start. The second is a test condition (i <= 50). When a conditional expression evaluates to true, the body of the loop is executed. When false, the loop terminates. The third part is an updater (i++) which is invoked after each iteration. The updater typically increments or decrements the loop counter.

My Code:

    //Function that returns HTML for successfully retrieved results
    function resultsHTML(JSONResponseObjectArray) {
        var ddObjectArray = JSONResponseObjectArray;
        var resultString = '';
        //Loop through JSON Object Array
        for(eeIndex in ddObjectArray) {
            var creditHours = ddObjectArray[eeIndex].creditHours;

        resultString += ceHTML(creditHours);
        console.log("credit hours: " + creditHours);

}

}

    function addCredits(){
      var result = 0;
        for (var i = 0; i <= 0; i++){
        result = creditHours + i;
        }
        return result;
          console.log('new results ' + result);
      }

Output of Array (via console):

credit hours: null
list.js:114 credit hours: 4
list.js:114 credit hours: 6
list.js:114 credit hours: null
list.js:114 credit hours: 4
list.js:114 credit hours: null
2
  • do you have any example array with JSON objects? Commented Mar 7, 2017 at 17:29
  • Couple small issues. First off, your addCredits function is inside of your other function. Second, resultsHTML isn't closed. Third, your console.log won't happen because it's after a return which ends the function. Commented Mar 7, 2017 at 17:33

1 Answer 1

2

You'd usually use reduce to sum the values in an array of objects (what your data will be - assumption as you don't show data). Just call the following addCredits() function with your array, replace b.credit with the property that holds the number to sum:

var creditHours = [{
  creditHours: 1
}, {
  creditHours: 2
}];

function addCredits(data) {
  return data.reduce(function(a, b) {
    a += b.creditHours;
    return a;
  }, 0)
}
console.log(addCredits(creditHours));

Sign up to request clarification or add additional context in comments.

16 Comments

I notice that the ` console.log("credit hours: " + creditHours);` from the first function is not working for the new function to add together.
I don't understand what you mean @thesayhey
I want to replace b.credit with ` var creditHours = ddObjectArray[eeIndex].creditHours;` but the console is complaining creditHours is not defined
There's no point of doing this. Replace b.credit with b.creditHours ... @thesayhey
shouldn't the console.log spit out creditHours as well ? and the json object be creditHours too?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.