Cash Register bug

Hi!
There is Wrong last test in Cash Register!!! Dont know where to write about it

1 Like

I have moved your post to a new thread.

Please do not post images of the code. Post the actual code instead.


When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Well, but i’m afraid of ban, bc someone can copy my code

** start of undefined **
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
      
</head>
<body>
    <input type="text" id="cash" name="name" value=""> </input>

<button id="purchase-btn"> PRESS </button>

<div id="change-due"></div>
<script src="script.js"></script>
</body>
</html>

** end of undefined **

** start of undefined **



** end of undefined **

** start of undefined **

let price = 19.5;
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

const text_input = document.querySelector("#cash")
const check_btn = document.querySelector("#purchase-btn")
const result = document.querySelector("#change-due")


check_btn.onclick = () => { 
   console.log(text_input.value)
   
  if (text_input.value < price) {
    alert('Customer does not have enough money to purchase the item') 
  }  else if (+text_input.value === price) {
    console.log(price)
    console.log(+text_input.value === price)
 result.innerHTML = 'No change due - customer paid with exact cash'
    } else {
      result.innerHTML = checkCashRegister(price, +text_input.value, cid)

      console.log(checkCashRegister(price, +text_input.value, cid))
    } 

}
 
function checkCashRegister(price, cash, cid) {
let change = cash - price

var obj_cid = Object.fromEntries(cid); 
let sum_cid = 0
let correct_str = []

// need an empty object to define and write only changed values,it helps a lot.

let money_sections_result = {
["ONE HUNDRED"] : 0,
["TWENTY"] : 0,
["TEN"] : 0,
["FIVE"] : 0,
["DOLLAR"] : 0,
["ONE"]: 0,
["QUARTER"] : 0,
["DIME"] : 0,
["NICKEL"] : 0,
["PENNY"] : 0
}



/////////////
// CALCULATE CYCLE START
//////////////
let prevChange

do {
let prevChange = change

switch(true) {
  case (change >= 100 && obj_cid["ONE HUNDRED"] >= 100): 
  change -= 100
  obj_cid["ONE HUNDRED"] -= 100
  money_sections_result["ONE HUNDRED"] += 100
  break
  case (change >= 20 && obj_cid["TWENTY"] >= 20): 
  change -= 20
  obj_cid["TWENTY"] -= 20
  money_sections_result["TWENTY"] += 20
  break
case (change >= 10 && obj_cid["TEN"] >= 10): 
  change -= 10
  obj_cid["TEN"] -= 10
  money_sections_result["TEN"] += 10
  break
  case (change >= 5 && obj_cid["FIVE"] >= 5): 
  change -= 5
  obj_cid["FIVE"] -= 5
  money_sections_result["FIVE"] += 5
  break
case (change >= 1 && obj_cid["ONE"] >= 1): 
  change -= 1
   obj_cid["ONE"] -= 1
  money_sections_result["ONE"] += 1
  break
  case (change >= 0.25 && obj_cid["QUARTER"] >= 0.25): 
  change -= 0.25
    obj_cid["QUARTER"] -= 0.25
  money_sections_result["QUARTER"] += 0.25
  break
    case (change >= 0.1 && obj_cid["DIME"] >= 0.1): 
  change -= 0.1
   obj_cid["DIME"] -= 0.1
  money_sections_result["DIME"] += 0.1
  break
      case (change >= 0.05 && obj_cid["NICKEL"] >= 0.05): 
  change -= 0.05
  obj_cid["NICKEL"] -= 0.05
  obj_cid["NICKEL"] = obj_cid["NICKEL"].toFixed(2)
  money_sections_result["NICKEL"] += 0.05
  money_sections_result["NICKEL"] = money_sections_result["NICKEL"].toFixed(2)
  break
case (change >= 0.01 && obj_cid["PENNY"] >= 0.01): 
  change -= 0.01
  obj_cid["PENNY"] -= 0.01
  obj_cid["PENNY"] = obj_cid["PENNY"].toFixed(2)
  money_sections_result["PENNY"] += 0.01
  money_sections_result["PENNY"] = Math.round(money_sections_result["PENNY"] * 100) / 100
  // console.log(obj_cid["PENNY"])
  break
}

change = Math.round(change * 100) / 100

if(change === prevChange) {
  return `Status: INSUFFICIENT_FUNDS`
}
let sum_cid = 0
for (let elem in obj_cid) {
  sum_cid += obj_cid[elem]
  sum_cid = Math.round(sum_cid * 100) / 100
}


// calculate closed status, task have a special conditions for it > 
if (sum_cid === 0 && change === 0) {

let arr_result = Object.entries(money_sections_result)

// arr_result.splice(0, 9)

arr_result.forEach(elem => {
    correct_str.push(`${elem[0]}:`)
     correct_str.push(`\$${elem[1]}`)
})
  return `Status: OPEN ${correct_str.join(' ')}`
}

} while (change > 0)
/////////////
// CYCLE END
//////////////

for(let elem in money_sections_result) {
  if (money_sections_result[elem] === 0) {
    delete money_sections_result[elem]
  }
}
// console.log(money_sections_result) 

let arr_result = Object.entries(money_sections_result)

// console.log(arr_result)
// console.log(arr_result.join(' '))
arr_result.forEach(elem => {
  console.log(typeof elem)
    correct_str.push(`${elem[0]}:`)
     correct_str.push(`\$${elem[1]}`)
})

// console.log(correct_str.join(' '))

if (change === 0) {
  return `Status: OPEN ${correct_str.join(' ')}`
}
}


** end of undefined **

I dont have problems, it needs to be fixed for others

It does look like a bug in the test.

It looks like the expected array is wrong in the test. It also doesn’t seem to account for the output containing more than it should only that it contains at least the expected.

https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-cash-register-project/build-a-cash-register.md

I have not looked at any of the new challenges or tests.

I opened an issue for it.