After get certification: I would like some explanation about my solution

Hello,
I would like some explanation about one problem I had with on project but I think I don’t know if I have the rigth to put the solution here even with Blur Spoiler.
Do I have the rigth or not ?

Yeah, blurring is sufficient. We know the solutions are out there, we just don’t want people to come across them by accident, if we can avoid it. Within the challenge, you can use the Get Help => Ask for Help button to post the code and a link to the challenge. Just please add [spoiler][/spoiler] tags for working solutions.

Ok,
Cash Register
First I get the certification. :partying_face:
This is my code:

function checkCashRegister(price, cash, cid) {
  const inCid = [...cid]; // coins and bills in store checkout. (pièces et billets en caisse.)
  const result = {status: '', change: []}
  let toRefund = cash - price; // the amount required. (La somme à rendre.)
  let totalCid = 0; // Total in store checkout. (Total en caisse.)
  console.log('A rendre(toRefund) : ' + toRefund)
  console.log('En caisse avant (inCid before) : ' + JSON.stringify(inCid))
  const coinBill = [0.01 , 0.05, 0.10, 0.25, 1, 5, 10, 20, 100];
  const changes = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]

for (let j = 0; j < inCid.length; j++ ) {
  totalCid = +(totalCid + inCid[j][1]).toFixed(2)
}
console.log('Totale en caisse avant(Total inCid before): ' + totalCid)
  for (let i = coinBill.length-1; i >= 0; i--) {
    if (toRefund > coinBill[i] && inCid[i][1] !== 0) {
      do {
        toRefund = +(toRefund - coinBill[i]).toFixed(2);
        inCid[i][1] = +(inCid[i][1] - coinBill[i]).toFixed(2);
        changes[i][1] = +(changes[i][1] + coinBill[i]).toFixed(2);
      } while (inCid[i][1] > 0 && toRefund >= coinBill[i])
  }
 }
 totalCid = 0;
 for (let j = 0; j < inCid.length; j++ ) {
  totalCid = +(totalCid + inCid[j][1]).toFixed(2)
  console.log(totalCid)
}
 console.log('Total en caisse après(Total inCid after) : ' + totalCid)

 console.log('Moneys rendue (changes done) : ' + JSON.stringify(changes))
 console.log('en caisse après (inCid after): ' + JSON.stringify(inCid))
 console.log('reste à rendre(toRefund) : ' + toRefund)
 
 if (toRefund > 0) {
   result.status = "INSUFFICIENT_FUNDS";
   result.change = [];
 } else if (totalCid < 0.01) {
   result.status = "CLOSED";
   result.change = [...changes]
 } else {
   result.status = "OPEN";
   result.change = [...changes].reverse().filter(item => item[1] !== 0)
 }
 console.log(JSON.stringify(result))
 return result
}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

I had some problem of calcul with comma.
I found this kind of solution: ‘totalCid = +(totalCid + inCid[j][1]).toFixed(2)’
It seam that work well because numbers are rounded, but in fact I don’t well understand how this works. Do you have explanation ?
Furthermore, despite this if I replace } else if (totalCid < 0.01) { by } else if (totalCid = 0) { that doesn’t work.
So I think there must be another solution.

And a link to the challenge please. We don’t have them all memorized.

I don’t understand what you are saying.

I found this kind of solution: ‘totalCid = +(totalCid + inCid[j][1]).toFixed(2)’
It seam that work well because numbers are rounded, but in fact I don’t well understand how this works. Do you have explanation ?

Numbers in JS do not allow you to set “places” - you get what you is calculated. There are different ways to deal with that. One technique is that you can convert it to a string and tell it how many places you need.

So totalCid + inCid[j][1]’ returns a number. Then _toFixed_ converts it to a string representation of the number with only two decimal places. Then the +` converts it to a number. So, you get a number. Another common technique to get two decimal places is to multiply by 100, round it, then divide by 100.

Furthermore, despite this if I replace } else if (totalCid < 0.01) { by } else if (totalCid = 0) { that doesn’t work.

Well, first of all, totalCid = 0 is not doing what you think it does. It is the assignment operator, not a comparison. But even with the comparison, it may be because there is some rounding error somewhere.

So I think there must be another solution.

There always are. But you have a working solution. Be proud of that. But sometimes it is cool to see how other people solved it.

This is a particularly nasty challenge. It drives a lot of people crazy - I know it did for me. I wouldn’t worry about perfection. Just take what you’ve learned and move on.

Thank you kevin for your answer.
I will make a donation because I think FreeCodeCamp site is wery well, you make a very good job.
Thank you for your explanations very clear and also for your quick answers along my trainning. It’s very importante to learn in confortable conditions.

Yeap! ofcourse, it was very late yesterday… it’s due to that :stuck_out_tongue_closed_eyes:.
that’s work well with: } else if (totalCid == 0) {

Now I plan to get “HTML / CSS” certification. This will be quick. Then “Front end development libraries” certification.
I would like to know if it was suficient to get a job as ‘front end deveveloper’ or if It was necessary to get another certifications ?

I little be afraid to forget after the time I will passed with the other certifications. So I would like to know how I can improve my Javascript ?

Do I have to do on particular FreeCodeCamp certification in parallel or have you some advices for me ?

Don’t worry about “quick” - worry about steady progress.

I would like to know if it was suficient to get a job as ‘front end deveveloper’ or if It was necessary to get another certifications ?

This is an impossible question to answer. There are too many factors. But in general, I think you’re at least going to want the first 6 certs. For me, it took another year of learning and building beyond that. #ymmv

I little be afraid to forget after the time I will passed with the other certifications.

You’re going to forget some details - that’s fine. That’s why we have google. Professional devs are looking up things all the time.

So I would like to know how I can improve my Javascript ?

Write JS. In the later certs you’ll get a lot of JS practice.

Do I have to do on particular FreeCodeCamp certification in parallel or have you some advices for me ?

I’m not sure I understand the question. But my advice would be just to keep learning and building.

Ok, the first 6 certs. This will be my aim now. Thank you again Kevin :wink:

Cool. Then you keep learning things, building more complex projects, and start worrying about applying and interviewing.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.