I am working on this code long time and still can't figure out what is wrong here... when I click on check code it says that:
- If I tried to remove 25 items, there should still be 20 items left in the inventory
- If I remove 20 items, you should say "All Out!"
but my code is working in both situations. So here is my code:
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 number=readInt("How many would you like to buy?");
numItems-=number;
if(numItems>0){
println("Now we have "+numItems+" left");
println("");
}
if(numItems==0){
println("");
println("All Out!");
}else if(numItems<0){
println("There is not enough in inventory for that purchase");
}
}
}
Please help me with this problem
I am working on this code long time and still can't figure out what is wrong here... when I click on check code it says that:
- If I tried to remove 25 items, there should still be 20 items left in the inventory
- If I remove 20 items, you should say "All Out!"
but my code is working in both situations. So here is my code:
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 number=readInt("How many would you like to buy?");
numItems-=number;
if(numItems>0){
println("Now we have "+numItems+" left");
println("");
}
if(numItems==0){
println("");
println("All Out!");
}else if(numItems<0){
println("There is not enough in inventory for that purchase");
}
}
}
Please help me with this problem
Share Improve this question edited Feb 1, 2021 at 19:01 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Feb 1, 2021 at 18:59 ne kone ko 331 gold badge1 silver badge3 bronze badges 5-
1
You immediately change
numItems
when you get an input, so when you ask for 25 it will never go back to 20. – Pointy Commented Feb 1, 2021 at 19:02 - Ok but when I enter 20, output will be "All Out!". So why second is wrong? – ne ko Commented Feb 1, 2021 at 19:15
-
1
You need to perform the subtraction and store the result in a temporary variable instead of immediately updating
numItems
. Check that temporary variable, make your decision about whether it's valid, and only updatenumItems
if it is. – Pointy Commented Feb 1, 2021 at 19:16 - So if I write e.g var result= numItems-number; it will be correct? – ne ko Commented Feb 1, 2021 at 19:19
- @neko Is this JavaScript or Java? I think this is actually Java. Remember Java is to JavaScript as car is to carpet. – gen_Eric Commented Feb 1, 2021 at 20:47
2 Answers
Reset to default 0You tagged your post javascript
but your code is not.
I bet it's your homework for tomorrow...
I rewrote your code to fit js needs :
var STARTING_ITEMS_IN_INVENTORY = 20;
function start() {
var numItems = STARTING_ITEMS_IN_INVENTORY;
while (numItems > 0) {
alert("We have " + numItems + " items in inventory");
var number = prompt("How many would you like to buy?");
numItems -= number;
if (numItems > 0) {
alert("Now we have " + numItems + " left");
} else if (numItems == 0) {
alert("All Out!");
} else if (numItems < 0) {
alert("There is not enough in inventory for that purchase");
}
}
}
start();
I give you the following code, that's what you want, I advise you to modify it to understand how algorithmic and js work.
var STARTING_ITEMS_IN_INVENTORY = 20;
function start() {
var numItems = STARTING_ITEMS_IN_INVENTORY;
while (numItems > 0) {
var number = prompt("How many would you like to buy? (" + numItems + " left)");
if (numItems - number > 0) {
numItems -= number;
} else if (numItems - number == 0) {
numItems -= number;
} else if (numItems - number < 0) {
alert("There is not enough in inventory for that purchase, please retry");
}
}
alert("All Out!");
}
start();
advises and infos :
readLn()
andprintLn()
are not core functions in javascript- in web dev you should not use
prompt()
noralert()
for that purpose - check the future item count of the inventory before asign it to the numItems
- read https://www.w3schools./js/DEFAULT.asp
I found this to be the simplest way to satisfy the autochecker.
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 howMany = readInt("How many items would you like to buy? ");
if(howMany > numItems){
println("There is not enough in inventory for that purchase.");
println("");
} else {
numItems -= howMany;
println("Now we have " + numItems + " left.");
println("");
}
}
println("All Out!");
}