Cash Register Challenge Guide help

Continuing the discussion from freeCodeCamp Challenge Guide: Cash Register:

I’ve been working on this algorithm for five days. At one point, I attempted to modify the solution posted by another user, but the tests still failed. Today I directly copy/pasted (without modifying at all) SEVERAL of the solutions given by users, so I would know if it was worth looking at further. NONE of them passed any of the tests. Is this an intentional method used for this forum? The only solution I tried that passes the tests is the “official” solution in the original guide post. I’m really confused.

The challenges receive updates periodically, so copying answers you find may result in the tests not passing.


It’s also against the academic honesty policy for projects to submit copied answers, btw.

Right, I wasn’t intending to submit those answers as my solution, I just didn’t understand why they didn’t work! I wanted to have something that works to use as an example, I’m really struggling with the task. Does that make sense?

1 Like

freeCodeCamp’s challenges evolve over time because we are always trying to make them more effective. You may have found solutions that worked for a slightly older version of the challenge. It’s also possible that there was something else going on. We do try to prevent people from sharing working solutions on the forum outside of specific situations.

If you plan on claiming the certificate, you’ll want to redo this project on your own. I hope that if you get stuck, you’ll feel comfortable sharing your in-progress solution here. We’d be happy to help get you unstuck.

3 Likes

Thanks @ArielLeslie, I’ll do that.

Understanding other people’s code and reverse engineering is an important development skill, but it’s not a skill we really want people practicing on the projects. It’s really recommended that you only look at solutions after you finish the project.

We’re more than happy to help you fix issues or answer questions as you develop your own solution. Learning to problem solve and develop code is a hard skill that takes practice!

1 Like

Ok @JeremyLT thanks for the feedback.

I’m sorry about that. This is my solution…

In the process, I encountered a precision problem, so I used the function to solve it.

function checkCashRegister (price, cash, cid) {

  var change;

  let ret = cash - price;

  const total = cid.reduce((p, c) => accAdd(p, c[1]), 0);

  if (ret == total) {

    change = { status: "CLOSED", change: cid }

  } else if (ret > total) {

    change = { status: "INSUFFICIENT_FUNDS", change: [] }

  } else {

    const M1 = {

      PENNY: 0.01,

      NICKEL: 0.05,

      DIME: 0.1,

      QUARTER: 0.25,

      ONE: 1,

      FIVE: 5,

      TEN: 10,

      TWENTY: 20,

      "ONE HUNDRED": 100,

    }

    const M2 = cid.reduce((p, c) => { p[c[0]] = c[1]; return p; }, {})

    const retChange = [];

    ["ONE HUNDRED", "TWENTY", "TEN", "FIVE", "ONE", "QUARTER", "DIME", "NICKEL", "PENNY"].forEach(unit => {

      let ret1;

      if (!+ret) return;

      if (M2[unit] && (ret1 = Math.floor(accDiv(ret, M1[unit])))) {

        const ret2 = accMul(ret1, M1[unit])

        if (M2[unit] >= ret2) {

          retChange.push([unit, ret2])

          ret = accSub(ret, ret2)

        } else {

          retChange.push([unit, M2[unit]])

          ret = accSub(ret, M2[unit])

        }

      }

    })

    if (+ret) {

      change = { status: "INSUFFICIENT_FUNDS", change: [] }

    } else {

      change = { status: "OPEN", change: retChange }

    }

  }

  return change;

}

function accAdd (arg1, arg2) {

  var r1, r2, m, c;

  try {

    r1 = arg1.toString().split(".")[1].length;

  }

  catch (e) {

    r1 = 0;

  }

  try {

    r2 = arg2.toString().split(".")[1].length;

  }

  catch (e) {

    r2 = 0;

  }

  c = Math.abs(r1 - r2);

  m = Math.pow(10, Math.max(r1, r2));

  if (c > 0) {

    var cm = Math.pow(10, c);

    if (r1 > r2) {

      arg1 = Number(arg1.toString().replace(".", ""));

      arg2 = Number(arg2.toString().replace(".", "")) * cm;

    } else {

      arg1 = Number(arg1.toString().replace(".", "")) * cm;

      arg2 = Number(arg2.toString().replace(".", ""));

    }

  } else {

    arg1 = Number(arg1.toString().replace(".", ""));

    arg2 = Number(arg2.toString().replace(".", ""));

  }

  return (arg1 + arg2) / m;

}

function accSub (arg1, arg2) {

  var r1, r2, m, n;

  try {

    r1 = arg1.toString().split(".")[1].length;

  }

  catch (e) {

    r1 = 0;

  }

  try {

    r2 = arg2.toString().split(".")[1].length;

  }

  catch (e) {

    r2 = 0;

  }

  m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度

  n = (r1 >= r2) ? r1 : r2;

  return ((arg1 * m - arg2 * m) / m).toFixed(n);

}

function accMul (arg1, arg2) {

  var m = 0, s1 = arg1.toString(), s2 = arg2.toString();

  try {

    m += s1.split(".")[1].length;

  }

  catch (e) {

  }

  try {

    m += s2.split(".")[1].length;

  }

  catch (e) {

  }

  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);

}

function accDiv (arg1, arg2) {

  var t1 = 0, t2 = 0, r1, r2;

  try {

    t1 = arg1.toString().split(".")[1].length;

  }

  catch (e) {

  }

  try {

    t2 = arg2.toString().split(".")[1].length;

  }

  catch (e) {

  }

  r1 = Number(arg1.toString().replace(".", ""));

  r2 = Number(arg2.toString().replace(".", ""));

  return (r1 / r2) * Math.pow(10, t2 - t1);

}

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]])

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

It seems that I don’t quite understand what you mean. Shouldn’t I publish a complete solution?

FCC is a learning platform.

Whenever, you make posts here you should help users by guiding them with their solutions.
Not posting your own and having them look at it to make theirs work.

I understand that you are trying to help but users will learn more if they understand how to make their code work instead of copying yours.

Hope that makes more sense!

2 Likes