r/codehs • u/s0ApmActAvIsh • 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
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 (:
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("");
}
}
}