Problem with last Data Structures Projects

Hi, here i post a screenshot of my code in the last challenge of Data Structures Projects. I really dont know where i do wrong, hope in some hints!

Please don’t post pictures of code. Please cut and paste the code and put three backticks on the line before and the line after so it formats.

And provide a link to the challenge.

In the future, the Get Help -> Ask for Help button will do that for you.

1 Like

the code:

function Oggetto() {

    this.status = "" ,

    this.change = [];   

       } ;

let array = [

 ["PENNY", 1.01],

 ["NICKEL", 2.05],

 ["DIME", 3.1],

 ["QUARTER", 4.25],

 ["ONE", 90],

 ["FIVE", 55],

 ["TEN", 20],

 ["TWENTY", 60],

 ["ONE HUNDRED", 100]

 ];

function checkCashRegister(price, cash, cid) {

    

     if (price > cash) {

        let primoOggetto = new Oggetto() ;

        primoOggetto.status = "\"INSUFFICIENT_FUNDS\"" ;

        return primoOggetto;

       }

    if (price = cash) {

        let secondoOggetto = new Oggetto() ;

        secondoOggetto.status = "\"CLOSED\"" ;

        secondoOggetto.change = cid;

       return secondoOggetto

       }

    if ( cash > price)    {

        let terzoOggetto = new Oggetto() ;

        terzoOggetto.status = "\"OPEN\"" ;

        terzoOggetto.change = array ;

        return terzoOggetto;

    }

 

 }

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

link : https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

I’ve edited your post for readability. 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 (’).

ok, here s some modifies, hope for a hint

function Oggetto() {
   this.status = "" ,
   this.change = [];   
      } ;
let array = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
];




function checkCashRegister(price, cash, cid) {
   
    if (price > cash) {
       let primoOggetto = new Oggetto() ;
       primoOggetto.status = "\"INSUFFICIENT_FUNDS\"" ;
       return primoOggetto;
      }
   if (price = cash) {
       let secondoOggetto = new Oggetto() ;
       secondoOggetto.status = "\"CLOSED\"" ;
       secondoOggetto.change = cid;
      return secondoOggetto

      }
   if ( cash > price)    {
       let terzoOggetto = new Oggetto() ;
       terzoOggetto.status = "\"OPEN\"" ;
      for (let i = 0; i < array.length; i++) {
         for (let j = 0; j < array[i].length; j++){
            let resto = cash - price ;
            
         }         
   }
}}

This is a hard challenge. Don’t get frustrated.

Just some quick observations:

let primoOggetto = new Oggetto() ;

And object constructor seems like overkill here.

primoOggetto.status = "\"INSUFFICIENT_FUNDS\"" ;

Don’t include the escaped quotes here. The string does not need quotes in it.

if (price = cash) {

Is that the right operator?

ok, i ll work on it thanks

Sure, fix those and then check back if you run into more trouble.

And again, this one stumps a lot of people. I remember struggling with it when I did it years ago. When I think of “pain in the ass challenges”, this is the one that comes to mind.

thank you for your support

hi, i think more sintax errors than logical errors, can you help me please?

link: https://www.freecodecamp.org/italian/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

let array = [["ONE HUNDRED", 100],["TWENTY", 20],["TEN", 10],["FIVE", 5],["ONE", 1],["QUARTER", 0.25],["DIME", 0.1],["NICKEL", 0.05],["PENNY", 0.01]];

function checkCashRegister(price, cash, cid) {

   let resto = cash - price;

   resto100 = resto / 100;

   resto20 = resto100 / 20;

   resto10 = resto20 / 10;

   resto5 = resto10 / 5;

   resto1 = resto5 / 1;

   resto025= resto1 / 0.25;

   resto01= resto025 / 0.1;

   resto005 = resto01/ 0.05;

   resto001= resto005/ 0.01;

   let newArray = [resto100, resto20, resto10, resto5, resto1,resto025, resto01,resto005, resto001]

    if (price = cash) {

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

       return firstObj

   }

   else {

      for(let i = 0; i < cid.length; i++) {

        for (let j =0; j < cid[i].length; j++) {

          let sommaCassa = reduce(cid[j])

           for ( sommaCassa > resto && cash < price) {

               cid[i].pop()

               cid[0].push(resto100)

               cid[1].push(resto20)

               cid[2].push(resto10)

               cid[3].push(resto5)

               cid[4].push(resto1)

               cid[5].push(resto025)

               cid[6].push(resto01)

               cid[7].push(resto005)

               cid[8].push(resto001)

               var secondObj = { status: "OPEN", change: cid }

                return secondObj;

            }

          for (sommaCassa < resto) {

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

             return terzoObj;

          }

        }

  

   }}}

edit: some useful changes, can you give some hints?

let array = [["ONE HUNDRED", 100],["TWENTY", 20],["TEN", 10],["FIVE", 5],["ONE", 1],["QUARTER", 0.25],["DIME", 0.1],["NICKEL", 0.05],["PENNY", 0.01]];

function checkCashRegister(price, cash, cid) {
   let resto = cash - price;  //x=1355 - 203.54=1151.46
   let ratio100 = resto / 100; // 1151.46 / 100 = 11.5146
   let pezzi100 = parseInt(ratio100) * 100; //11 * 100=1100
   let resto20 = resto - pezzi100; // 1151.46 -1100 = 51.46
   let ratio20= resto20 / 20;    //51.46 / 20= 2.573
   let pezzi20 = parseInt(ratio20) * 20;  // 2 * 20 = 40
   let resto10 = resto - pezzi100 - pezzi20;  //1151.46 - 1100 - 40 =11.46
   let ratio10 = resto10 / 10;  //11.46 / 10 = 1.146
   let pezzi10 = parseInt(ratio10) * 10;  // 1 * 10 = 10
   let resto5 = resto - pezzi100 - pezzi20 - pezzi10;  //1151.46 - 1100 - 40 - 10
   let ratio5 = resto5 / 5;
   let pezzi5 = parseInt(ratio5) * 5;
   let resto1 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5;
   let ratio1 = resto1 / 1;
   let pezzi1 = parseInt(ratio1) * 1;
   let resto025 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1;
   let ratio025 = resto025 / 0.25;
   let pezzi025 = parseInt(ratio025) * 0.25
   let resto01 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025;
   let ratio01 = resto01 / 0.1;
   let pezzi01 = parseInt(ratio01) * 0.1
   let resto005 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025 - pezzi01;
   let ratio005 = resto005 / 0.05;
   let pezzi005 = parseInt(ratio005) * 0.05;
   let resto001 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025 - pezzi01 - pezzi005;
   let ratio001 = resto001 / 0.01;
   let pezzi001 = parseInt(ratio001) * 0.01;


    if (price = cash) { 
       var firstObj = { status: "CLOSED", change: cid}
       return firstObj
   } 

   else {
      for(let i = 0; i < cid.length; i++) {
        for (let j =0; j < cid[i].length; j++) {

          var sommaCassa = reduce(cid[j])

           if ( sommaCassa > resto && cash < price) {
               array[i].pop() 
               array[0].push(pezzi100)
               array[1].push(pezzi20)
               array[2].push(pezzi10)
               array[3].push(pezzi5)
               array[4].push(pezzi1)
               array[5].push(pezzi025)
               array[6].push(pezzi01)
               array[7].push(pezzi005)
               array[8].push(pezzi001)
               var secondObj = { status: "OPEN", change: array }
                return secondObj;
            }

          if (sommaCassa < resto) {
             var terzoObj = {status: "INSUFFICIENT_FUNDS", change: cid}
             return terzoObj;
          }

        }
 
 

   }}}


Hi everybody, im facing with last javascript challenge, the one of cash register, i will be thankfull i you could give me help. here s my code:

let array = [["ONE HUNDRED", 100],["TWENTY", 20],["TEN", 10],["FIVE", 5],["ONE", 1],["QUARTER", 0.25],["DIME", 0.1],["NICKEL", 0.05],["PENNY", 0.01]];

function checkCashRegister(price, cash, cid) {
   let resto = cash - price;  //x=1355 - 203.54=1151.46
   let ratio100 = resto / 100; // 1151.46 / 100 = 11.5146
   let pezzi100 = parseInt(ratio100) * 100; //11 * 100=1100
   let resto20 = resto - pezzi100; // 1151.46 -1100 = 51.46
   let ratio20= resto20 / 20;    //51.46 / 20= 2.573
   let pezzi20 = parseInt(ratio20) * 20;  // 2 * 20 = 40
   let resto10 = resto - pezzi100 - pezzi20;  //1151.46 - 1100 - 40 =11.46
   let ratio10 = resto10 / 10;  //11.46 / 10 = 1.146
   let pezzi10 = parseInt(ratio10) * 10;  // 1 * 10 = 10
   let resto5 = resto - pezzi100 - pezzi20 - pezzi10;  //1151.46 - 1100 - 40 - 10
   let ratio5 = resto5 / 5;
   let pezzi5 = parseInt(ratio5) * 5;
   let resto1 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5;
   let ratio1 = resto1 / 1;
   let pezzi1 = parseInt(ratio1) * 1;
   let resto025 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1;
   let ratio025 = resto025 / 0.25;
   let pezzi025 = parseInt(ratio025) * 0.25
   let resto01 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025;
   let ratio01 = resto01 / 0.1;
   let pezzi01 = parseInt(ratio01) * 0.1
   let resto005 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025 - pezzi01;
   let ratio005 = resto005 / 0.05;
   let pezzi005 = parseInt(ratio005) * 0.05;
   let resto001 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025 - pezzi01 - pezzi005;
   let ratio001 = resto001 / 0.01;
   let pezzi001 = parseInt(ratio001) * 0.01;


    if (price = cash) { 
       var firstObj = { status: "CLOSED", change: cid}
       return firstObj
   } 

   else {
      for(let i = 0; i < cid.length; i++) {
        for (let j =0; j < cid[i].length; j++) {

          var sommaCassa = reduce(cid[j])

           if ( sommaCassa > resto && cash < price) {
               array[i].pop() 
               array[0].push(pezzi100)
               array[1].push(pezzi20)
               array[2].push(pezzi10)
               array[3].push(pezzi5)
               array[4].push(pezzi1)
               array[5].push(pezzi025)
               array[6].push(pezzi01)
               array[7].push(pezzi005)
               array[8].push(pezzi001)
               var secondObj = { status: "OPEN", change: array }
                return secondObj;
            }

      

        }
 
     if (sommaCassa < resto) {
             var terzoObj = {status: "INSUFFICIENT_FUNDS", change: cid}
             return terzoObj;
          }

   }}}

Floating Point Error - dove hai a che fare con i decimali, non funzionerĂ  mai come vuoi

consiglio: lavora in numero di centesimi

Ma comunque, puoi descrivere il processo logico che stai usando? non credo di seguirlo

e per favore crea un solo topic per sfida

ciao, scusatemi ne ho creato un altro topic prima non avevo visto la tua risposta, qui ti posto il mio codice attuale: #EDIT: nuovo ultimo post cancellato.

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

function checkCashRegister(price, cash, cid) {
   let resto = cash - price;  
   let ratio100 = resto / 100; 
   let pezzi100 = parseInt(ratio100) * 100; 
   let resto20 = resto - ratio100; 
   let ratio20= resto20 / 20;    
   let pezzi20 = parseInt(ratio20) * 20; 
   let resto10 = resto - ratio100 - ratio20; 
   let ratio10 = resto10 / 10;  
   let pezzi10 = parseInt(ratio10) * 10;  
   let resto5 = resto - ratio100 - ratio20 - ratio10;  
   let ratio5 = resto5 / 5;
   let pezzi5 = parseInt(ratio5) * 5;
   let resto1 = resto - ratio100 - ratio20 - ratio10 - ratio5;
   let ratio1 = resto1 / 1;
   let pezzi1 = parseInt(ratio1) * 1;
   let resto025 = resto - ratio100 - ratio20 - ratio10 - ratio5 - ratio1;
   let ratio025 = resto025 / 0.25;
   let pezzi025 = parseInt(ratio025) * 0.25
   let resto01 = resto - ratio100 - ratio20 - ratio10 - ratio5 - ratio1 - ratio025;
   let ratio01 = resto01 / 0.1;
   let pezzi01 = parseInt(ratio01) * 0.1
   let resto005 = resto - ratio100 - ratio20 - ratio10 - ratio5 - ratio1 - ratio025 - ratio01;
   let ratio005 = resto005 / 0.05;
   let pezzi005 = parseInt(ratio005) * 0.05;
   let resto001 = resto - ratio100 - ratio20 - ratio10 - ratio5 - ratio1 - ratio025 - ratio01 - ratio005;
   let ratio001 = resto001 / 0.01;
   let pezzi001 = parseInt(ratio001) * 0.01;
   
   
  var ultimoArray = [["ONE HUNDRED", pezzi100],["TWENTY", pezzi20],["TEN", pezzi10],["FIVE", pezzi5],["ONE", pezzi1],["QUARTER", pezzi025],["DIME", pezzi01],["NICKEL", pezzi005],["PENNY", pezzi001]];
  
  
  var sommaCassa = cid.reduce(function (prev,next) {
  return prev + next[1];
  },0);


   if (price == cash | sommaCassa == resto) { 
       var firstObj = { status: "CLOSED", change: cid}
       return firstObj
   } 

   if (sommaCassa < resto) {
     
          var terzoObj = {status: "INSUFFICIENT_FUNDS", change: []}
    return terzoObj;
   }


   else if (sommaCassa>resto && cash> price){
    {     
               array[0].pop()  
               array[1].pop()
               array[2].pop()  
               array[3].pop()  
               array[4].pop()  
               array[5].pop()  
               array[6].pop() 
               array[7].pop()  
               array[8].pop()
               array[0].push(pezzi100);
               array[1].push(pezzi20);
               array[2].push(pezzi10);
               array[3].push(pezzi5);
               array[4].push(pezzi1);
               array[5].push(pezzi025)
               array[6].push(pezzi01)
               array[7].push(pezzi005)
               array[8].push(pezzi001)
               var secondObj = { status: "OPEN", change: array }
                return secondObj;
              
              }}
            
        }

il processo logico che sto usando è: il problema è l array da mettere in change, se ad esempio devo tornare 200euro, il primo array annidato dentro l array da mettere in change dovra essere [“ONE HUNDRED”, 200],
quindi faccio prima il calcolo del resto totale che devo tornare, lo divido per 100 per vedere quante volte ci entra in ogni baconota da 100, e poi prendo il numero intero e lo moltiplico per 100 per ottenere il valore delle baconote da 100 che mi servono per il resto, e cosi via per tutte le banconote. adesso nell mio ultimo codice i valore che non passano sono:

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
        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]])
        checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])

la prima tripletta di valore non va perche il resto che in questo caso sara un “QUARTER” e non è presente, in quanto è presente un centesimo e un penny, ma non posso dividere un penny ovviamente, quindi magari dovrei porre qualche condizione per fare in modo che tutti i secondi elementi degli array annidati di cid siano maggiori o uguali ai vari pezzi100,pezzi20,pezzi10,…e cosi via.
vista la strada che ho preso non trovo alternativa, spero che tu mi puoi consigliare. Grazie

l unica cosa che mi è venuta in mente per sistemare l array da ridare in change è:
-creare un array da cid che mi dia solo i valori che ho nella cassa (quindi solo i secondi elementi degli array annidati)
-creare un array da ultimoArray che mi dia solo i suoi secondi elementi, quindi i pezzi delle varie banconote che mi servono per il resto
e poi dopo aver itenerato i due array, porre cid[i]>=ultimoArray[j], se la condizone è valida allora si arriverà all ultimo else if, se non è valida si fermerà allo status “INSUFFICIENT_FUNDS”. non so se è una strada fattibile ma ora provo, aspetto tuoi suggerimenti.

ok, consiglio di aggiungere console.log ad ogni riga di codice così vedi se sta facendo quello che ti aspetti

Ciao, ho aggiornato il codice. Ho aggiunto una function verifica() che mi dovrebbe prendere i singoli elementi del cash-in-drawer e i singoli valori delle banconote che devo dare come resto in modo che li compara tra di loro. Credo sia questo l unico ostacolo dal finire la challenge. Vi chiedo un aiuto o un suggerimento, ha da giorni che sono fermo qui :man_facepalming:

function checkCashRegister(price, cash, cid) {
var array = [["ONE HUNDRED", 100],["TWENTY", 20],["TEN", 10],["FIVE", 5],["ONE", 1],["QUARTER", 0.25],["DIME", 0.1],["NICKEL", 0.05],["PENNY", 0.01]];
let resto = cash - price;  
let ratio100 = resto / 100; 
let pezzi100 = parseInt(ratio100) * 100; 
let resto20 = resto - ratio100; 
let ratio20= resto20 / 20;    
let pezzi20 = parseInt(ratio20) * 20; 
let resto10 = resto - ratio100 - ratio20; 
let ratio10 = resto10 / 10;  
let pezzi10 = parseInt(ratio10) * 10;  
let resto5 = resto - ratio100 - ratio20 - ratio10;  
let ratio5 = resto5 / 5;
let pezzi5 = parseInt(ratio5) * 5;
let resto1 = resto - ratio100 - ratio20 - ratio10 - ratio5;
let ratio1 = resto1 / 1;
let pezzi1 = parseInt(ratio1) * 1;
let resto025 = resto - ratio100 - ratio20 - ratio10 - ratio5 - ratio1;
let ratio025 = resto025 / 0.25;
let pezzi025 = parseInt(ratio025) * 0.25
let resto01 = resto - ratio100 - ratio20 - ratio10 - ratio5 - ratio1 - ratio025;
let ratio01 = resto01 / 0.1;
let pezzi01 = parseInt(ratio01) * 0.1
let resto005 = resto - ratio100 - ratio20 - ratio10 - ratio5 - ratio1 - ratio025 - ratio01;
let ratio005 = resto005 / 0.05;
let pezzi005 = parseInt(ratio005) * 0.05;
let resto001 = resto - ratio100 - ratio20 - ratio10 - ratio5 - ratio1 - ratio025 - ratio01 - ratio005;
let ratio001 = resto001 / 0.01;
let pezzi001 = parseInt(ratio001) * 0.01;

var ultimoArray = [pezzi100, pezzi20, pezzi10, pezzi5, pezzi1, pezzi025, pezzi01,pezzi005, pezzi001];
var sommaCassa = cid.reduce(function (prev,next) {
    return prev + next[1];
    },0);
var nuovoCid = cid.flat();
   
let numeriCid = nuovoCid.filter(value => {
return typeof value === 'number';
});
var ultimoCid = numeriCid.reverse();
function verifica() {
   for(let i =0; i < ultimoCid.length; i++){
    for(let j =0; j < ultimoArray.length; j++)
     if (ultimoCid[i]>= ultimoArray[j]) {
        return true;
       } else {
          return false;
       }
}}

if (sommaCassa==resto ) { 
    var firstObj = { status: "CLOSED", change: cid} //OK
    return firstObj
} 

if (sommaCassa < resto | verifica()==false) {
                 
    var terzoObj = {status: "INSUFFICIENT_FUNDS", change: []}
return terzoObj;
}

else if (sommaCassa>resto && cash> price && verifica()==true ){
    {  array[0].pop()  
       array[1].pop()
       array[2].pop()  
       array[3].pop()  
       array[4].pop()  
       array[5].pop()  
       array[6].pop() 
       array[7].pop()  
       array[8].pop()
       array[0].push(pezzi100);
       array[1].push(pezzi20);
       array[2].push(pezzi10);
       array[3].push(pezzi5);
       array[4].push(pezzi1);
       array[5].push(pezzi025)
       array[6].push(pezzi01)
       array[7].push(pezzi005)
       array[8].push(pezzi001)
        var secondObj = { status: "OPEN", change: array }
         return secondObj;
              
              }}
            
        }

io continuo a consigliare di provare ad analizzare riga per riga il tuo codice
perché sto testando con il caso in cui dovresti restituire 0.5 in monete da 0.25

image

ma il tuo codice dice solo 0

ok, ora controllo grazie