#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Exclamation Easy Logic Question


    Hey guys we were assigned to do Logic homework via codingbat.

    I am really stuck on this particular part.


    Directions:
    When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given values is successful, or false otherwise.

    cigarParty(30, false) → false
    cigarParty(50, false) → true
    cigarParty(70, true) → true
    My following attempt at this code:
    PHP Code:
      public boolean cigarParty(int cigarsboolean isWeekend) {
     
     if( (
    cigars >= 40 && cigars <=60) && !isWeekend)
      {
      return 
    true;
      
      }
      
      else if( 
    cigars && cigars 100 && isWeekend)
      {
      return 
    true;
      }
      
      else {
      return 
    false;
      }
     
      

    My outcome:
    PHP Code:
       Expected    This Run        
    cigarParty
    (30false) &#8594; false    false    OK        
    cigarParty(50false) &#8594; true    true    OK        
    cigarParty(70true) &#8594; true    true    OK        
    cigarParty(30true) &#8594; false    true    X        
    cigarParty(50true) &#8594; true    true    OK        
    cigarParty(60false) &#8594; true    true    OK        
    cigarParty(61false) &#8594; false    false    OK        
    cigarParty(40false) &#8594; true    true    OK        
    cigarParty(39false) &#8594; false    false    OK        
    cigarParty(40true) &#8594; true    true    OK        
    cigarParty(39true) &#8594; false    true    X        
    other tests
            OK 
    I dont understand what I am doing wrong. Can anyone explain?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Katy, Texas
    Posts
    495
    Rep Power
    199
    Obviously, your logic is wrong.

    If it is the weekend, as long as the count is the minimum, the max count of cigars doesn't matter.

    Else, (it's not the weekend, and) the count of cigars should be within the range.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0
    Originally Posted by TheOtherDino
    Obviously, your logic is wrong.

    If it is the weekend, the count of cigars doesn't matter.

    Else, the count of cigars should be within the range.
    Thats the thing I am not sure how to write that in code.
    ive been struggle the past hour with no prevail

    How do we create the count doesnt matter?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0
    Originally Posted by DelaySix
    Thats the thing I am not sure how to write that in code.
    ive been struggle the past hour with no prevail

    How do we create the count doesnt matter?
    Okay never mind I got the program to work I was missing a part of it haha

IMN logo majestic logo threadwatch logo seochat tools logo