This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Javascript Code Not Working When Calling from a Prompt  RSS feed

 
John Winston
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See below for my code in Javascript that I have been working on in JSBIN for a while.  I cannot figure out how to fix it to make it work.  The purpose is to create a list of perishable items with various attributes that can be used for different helpful tasks.  I created the key attributes I wanted to use and then seeded it with 4 test variables (milk, eggs, bread & butter) with all the data just to be able to test the code worked.  Then I built a current date code to calculate in a format the date to see if an item is expired or not and be able to also work with the lastBuyDate attribute as well.  The problem issue is the last block of code "Calculate expiration of an item".  What I am trying to do is allow a prompt to pop up with a list of items that the user can type to select if it is expired or not expired.  Then I try to form it so that I can do a compare on that same method of my Perishable Food object variable.  My hope is to get it to compare the dates of the Perishable Food expire date to the current date (today).  Then I want to take that and push out a response that says if it is or is not expired.  I know that the user prompt and trying to create something like the method in this part of the code is where I am having the trouble but I really don't know how to solve for it to make it work.  When I do a console.log to compare userChoiceSelected to milk.expireDate (if user prompt is milk) it comes up as false instead of true which is why I think the code keeps failing and no matter the date responding with the "You don't need...." message vs. actually calculating it and printing the right message.  Thanks!!!



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66198
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Firstly, your opening paragraph is pretty long, which makes it really hard to slog through. Break up long text sequences into easily digestible paragraphs. After 3 or 4 lines, my aging eyes start to roll up into the back of my head.

Then, there are some strangenesses and oddities about your code that you need to think about:
  • Naming consistency. I see expiredDate and expirationDate. If they are supposed to be the same, they're obviously not. Why use two different names for the same thing?
  • Line 31 is perprlexing. You are createing an array with a single string created from the prompt answer and two strings. Why? I suspect that's not really what you are trying to do because it makes little sense.
  • The function you create at line 32 has a parameter that is never used, and an implicit input from the closure. Both of these are really bad code smells. Rethink what you are trying to do there, and how you are doing it.
  • Lastly (for now), you are storing dates as strings. Always a bad move when comparisons are involved. Use actual Date instances so that they can be deterministically compared.

  • You obviously know about the debugging console (you mention using console.log), but are you employing breakpoints to step through the code to see if variables contain what you think they do? and that the code is operating on them as expected?
     
    Michael Matola
    whippersnapper
    Ranch Hand
    Posts: 1838
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    On line 40 you are passing a reference to the PerishableFood (constructor) function to the function expired. Why? What are you expecting to happen?

    userChoice is a string, such as "milk".
    userChoiceSelected is an array whose first element is a string that is the result of concatenating the userChoice and ".expireDate".
    today started out as an actual date, then you converted it to a string representation of a date.

    So the comparison on line 33 (if(userChoiceSelected<=today)) compares an array to a string:



    Holy type coercion, Batman! JavaScript has some arcane rules for comparing things of different types. I don't even want to take a stab at what the JavaScript engine is doing in this case. And I'm sure the code as written is not what you're intending. (Were you hoping to use bracket notation for the property expireDate? If so, you got the syntax wrong for that and created an array instead.)

    (1) You explicity are using a string representation of a date instead of an actual date for comparison. Do not do this. Ever. Trouble and strife will follow. Use actual Date functions for operating on date. Don't convert today to a string to compare with the expiredDate value stored on the PerishableFood object. Instead, parse the stored expiredDate value into a Date and then compare it with today. (You're fine with <= comparison, but there's a JavaScript gotcha with date equality.)

    (2) I'm guessing that what you really want to do is compare the value of the expiredDate property of some PerishableFood object with the current date:



    or



    That makes sense to me. However, in the current state of the code, you don't have a handle on an object. All you have at that point is a string from user input (such as "milk") that just so happens to be a variable name in your code (!). The easiest way to get deal with something like this here is to create a simple data structure to hold the references to all of the PerishableFood objects you create in code, then use the text string from user input as a key to search that data structure. There are several ways of doing this. Using a plain old JavaScript object as a hash/dictionary/map with strings as keys is a simple approach.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!