Codehs Inventory: What am I doing wrong in this code?

Viewed 4152

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:

  1. If I tried to remove 25 items, there should still be 20 items left in the inventory
  2. 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

2 Answers

You 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() and printLn() are not core functions in javascript
  • in web dev you should not use prompt() nor alert() for that purpose
  • check the future item count of the inventory before asign it to the numItems
  • read https://www.w3schools.com/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!");
}
Related