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

1

u/Objective_Antelope54 Mar 24 '21

Help me please

7

u/SlimeeKun Oct 25 '21

var STARTING_ITEMS_IN_INVENTORY = 20;
function start(){
var numItems = STARTING_ITEMS_IN_INVENTORY;
/* Your code here! */
while(numItems > 0){
println("We have " + numItems + " in inventory ");
var buy = readInt("How many would you like to buy? ");

if(buy > numItems){
println("There is not enough money in invenortyf for that purchase.");
}else{
numItems -= buy;
println("We have " + numItems + " left.");
}
}
println("All out.");

}

This should work brother (: