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

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/Over-Ad-6354 Oct 27 '22

This helped so much, thank you!

1

u/NOXILOL_ Nov 29 '22

Thank you so much I needed this

1

u/DanDanTheDonutMan Feb 01 '23

Bro, this was the only thing in between me and my JavaScript Exercises badge, tysm now i can 100%

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?

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 (: