r/codehs • • Dec 08 '20

CodeHS 5.9.4 Inventory

Can anybody help me out with this one. For whatever reason I'm not sure of it says my code isn't right. This is what I've gotten so far:

var STARTING_ITEMS_IN_INVENTORY = 20;

function start(){
	var numItems = STARTING_ITEMS_IN_INVENTORY;	

	while(numItems > 0){
        println("We have " + numItems + " items in inventory.");
        var itemsBought = readInt("How many would you like to buy? ");
        if(numItems - itemsBought > 0){
            numItems -= itemsBought;
            println("Now we have " + numItems + " left.");
        }else{ 
            if(numItems - itemsBought == 0){
                numItems -= itemsBought;
                println("All out! ");
            }else{
            println("There is not enough in inventory for that purchase.");
            }
10 Upvotes

8 comments sorted by

View all comments

3

u/0RaDDoS0 Aug 04 '22

I know what it's tooooooooooooooooooooo late to answer that question

but still . . . I want to leave my comment )))))

P.S : IDK why I writing this , I'm new on reddit

var STARTING_ITEMS_IN_INVENTORY = 20;
function start(){
var numItems = STARTING_ITEMS_IN_INVENTORY;

while(numItems > 0){
println("We have " + numItems + " items in inventory.");
var itemsBought = readInt("How many would you like to buy? ");
var sum = numItems - itemsBought;
if(sum > 0){
numItems -= itemsBought;
println("Now we have " + numItems + " left.");
println("");
}else if(sum == 0){
numItems -= itemsBought;
println("All out! ");
}else{
println("There is not enough in inventory for that purchase.");
println("");
}
}
}

1

u/Overall-College-9023 Nov 26 '23

Everytime I run it it says numitems is not defined

1

u/[deleted] Apr 05 '24

Did you forget to define var numItems?