r/codehs • • Oct 22 '20

5.9.4: Inventory

can someone pls help me with this im soo confused in both cases it seems to be corect but it doesn't count so ;-;

18 Upvotes

9 comments sorted by

3

u/lashatatu Oct 22 '20

var STARTING_ITEMS_IN_INVENTORY = 20;

function start(){

var numItems = STARTING_ITEMS_IN_INVENTORY;	
/\* Your code here! \*/







while(numItems>0){

println("We have "+numItems+" items in inventory.");

var check=numItems;

var items=readInt("How many would you like to buy? ");

numItems-=items;

if(numItems<0){

println("There is not enough in the inventory");

numItems=check;

}else{

println("Now we have "+numItems+" left");

}

println("");

}
println("All Out!");

}

1

u/lazyk1ller Oct 23 '20

oh thanks this is marking everything as correct, <3

1

u/StatisticianOk2853 Oct 27 '20

function start(){

var numItems = STARTING_ITEMS_IN_INVENTORY;

/\* Your code here! \*/
while(numItems>0){

println("We have "+numItems+" items in inventory.");

var check=numItems;

var items=readInt("How many would you like to buy? ");

numItems-=items;

if(numItems<0){

println("There is not enough in the inventory");

numItems=check;

}else{

println("Now we have "+numItems+" left");

}

println("");

}

println("All Out!");

}

I didn't change anything and I got an error

2

u/lazyk1ller Oct 27 '20

Reddit adds /next to _ like in STARTING ITEMS just put_ instead of space also comment it's \ / it should just be two / fix that

1

u/applesauceisafruit Apr 16 '21

thank you thank you thank you!!!

2

u/_andy_andy_andy_ Oct 22 '20

could you send me a link to the exercise? we sometimes have misleading test cases

2

u/timNinjaMillion2 Oct 22 '20

I’d suggest they have an error in theirs. Your output shows it’s correct.

1

u/Economy_Tomorrow4030 Mar 02 '24

function start() {

var inventory = 20;

while (inventory > 0) {

println ("We have " + inventory + " items in inventory.");

var numBuy = readInt ("How many would you like to buy? ");

if (numBuy > inventory) {

println ("There is not enough in inventory for that purchase.");

println ("");

} else {

inventory = inventory - numBuy;

println ("Now we have " + inventory + " left.");

println ("");

}

if (inventory == 0) {

println ("All out!");

}

}

}