How to sum up specific array elements?

How to sum up specific array elements?
0

#41

Well, the course has a philosophy of “do it to learn it” coupled with “search-read-ask”. The former keeps those with lots of self-doubt and low frustration tolerance in the groove initially. The second is just standard practice for any programmer learning new things. The issue which I have with the course is that there is a moment in the Javascript section where a newcomer to coding needs to be lectured, and fed many new concepts in a piecemeal fashion. The “search-read-ask” will usually land someone on the forum at that point, and someone like me will point them to a free formal intro to CS class. If you look at the stories of those who got developer jobs, they near-universally did both FCC and a more structured didactic approach (more than 2 out 3 times it was CS50).

This challenge doesn’t need more than simple array processing to complete. The most complicated data structure required is an array. The harder part of it is formulating an algorithm (not coding it), and figuring out of which data you need to keep track. For example, you weren’t initially keeping track of how much change you had already made, but there is no way to do it without that. Now you have sum or changeMade or changeSoFar or changeMadeSoFar.

CS50 will tell you why some of the Array functions are called “push/pop” or “shift/unshift”. It’s because Javascript arrays do a lot that would be covered in basic data structures. I’m only through the front end, and I haven’t directly seen a mention of trees, but it’s implied in the DOM Tree discussions. As always, FCC is a work in progress. For some, that material would be review, for some, it would be too intimidating at first. But the forum, and advice discussions like these, is what plugs any holes.

You need two things to arrive at a passable solution:

  1. Fix the sum assignment.
  2. Remove unnecessary loops.

If you haven’t run my fiddle linked above yet, do it. Read the console, and you’ll see what I mean.


#42

Alright. I’ll do that and let you know once I am done. Thanks.


#43

So I was finally able to do it. At last. i know the code is not as efficient as it should be but it does work. Now I am thinking of starting the CS50 course and give this course FreeCodeCamp a break. Is that what I should do?

function checkCashRegister(price, cash, cid) {
  var cidtotal = 0;
  var cidValues = [];
  var changeMade = 0;
  var divider = [];
  var temp = 0;
  var finalArr = [];

  var currencyValues = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
  currencyValues.reverse();

  var change = cash - price;
  console.log("Change: " + change);

  for (var x = 0; x < cid.length; x++) {
    cidValues.push(cid[x][1]);
  }
  cidValues.reverse();

  cidtotal = cidValues.reduce(function(item, total) {
    return item + total;
  }, 0);

  console.log("Cid Total: " + cidtotal);

  if (change > cidtotal) {
    return {"status" : "INSUFFICIENT_FUNDS", "change" : []};
  }

  else if (change == cidtotal) {
    return {"status" : "CLOSED", "change" : cid};
  }

  else if (change < cidtotal) {
    for (var y = 0; y < currencyValues.length; y++) {
        if (currencyValues[y] >= change) {
        currencyValues.splice(y, 1);
        cidValues.splice(y, 1);
      }
    }
    console.log("Currency Values: " + currencyValues);
    console.log("Cid Values: " + cidValues);

    for (var i = 0; i < cidValues.length; i++) {
      divider.push(Math.round(cidValues[i] / currencyValues[i][1]));
    }
    for (var j = 0; j < currencyValues.length; j++) {
      temp = currencyValues[j][1] * divider[j];
        if (temp <= change) {
          if (temp !== 0){
          finalArr.push([currencyValues[j][0], temp]);
          }
          change = (change - temp).toFixed(2);
        }

        else {
          while (temp > change) {
            temp = currencyValues[j][1] * divider[j];
            divider[j]--;
          }
          if (temp !== 0){
          finalArr.push([currencyValues[j][0], temp]);
          }
          change = (change - temp).toFixed(2);
        }
    }
    finalArr.filter(function(item) {
      return item.temp !== 0;
    })
    console.log("Change Left: " + change);
    console.log(finalArr);
    if (change == 0) {
    return {"status" : "OPEN", "change" : finalArr};
    }

    else {
     return {status: "INSUFFICIENT_FUNDS", change: []};
    }

  }

}

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);

#44

Sorry for the delayed response, I’ve been away on a trip, and then taking care of a sick family member. Yes, I believe you would be greatly helped in your growth by some formal CS training. Learning to think in a structured way about how to approach problems will help you tremendously at this point in your learning. Come back to FCC for practice. You can interweave the learning so you keep up your HTML/CSS skills.

As for efficient code, if you come back to any code after some time, you’ll know more, and be able to think better about how to write clean, performant code. I know that it was that way for me. Good luck!


#45

Thank you for the advice.