how can I get if equal to 0 work on if statment

Discussion in 'JavaScript' started by macaela, Oct 27, 2012.

  1. #1
    Hi I am trying to do an if statment that check
    if value more or equal to 0 or less or equal to 100 to work but it only accepts part of the condition

    here my code

    // Ex: 3-1 Grading Work
    var goal;
    var grade;
    grade = "Not Yet Graded";
    
    goal = prompt( "Please input the goal(%)" );
    goal = parseInt( goal, 10 ); // see comment
    
    
     
    if ((goal ==100 || goal > 100) || (goal < 0 || goal === 0)) 
    
    {
    	
    	if( goal > 70 )
    	{
    		grade = "First Class";
    		alert( "goal: " + goal + "% - Grade: " + grade );
    	}
    
    	else if( goal > 60 )
    	{
    		grade = "2.1";
    		alert( "goal: " + goal + "% - Grade: " + grade );
    	}
    	else if( goal > 50 )
    	{
    		grade = "2.2";
    		alert( "goal: " + goal + "% - Grade: " + grade );
    	}
    	else if( goal > 40 )
    	{
    		grade = "Third Class ";
    		alert( "goal: " + goal + "% - Grade: " + grade );
    	}
    	else if ( goal < 40 )
    	{
    		grade = "Fail";
    		alert( "goal: " + goal + "% - Grade: " + grade );
    	}
    	else if ( goal < 40 )
    	{
    		grade = "Fail";
    		alert( "goal: " + goal + "% - Grade: " + grade );
    	}
    }
    
    else
    {
    	alert( "Invalid goal, outside range 0-100" );
    }
    
    
    Code (markup):

     
    macaela, Oct 27, 2012 IP
  2. knewedge

    knewedge Active Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #2
    Use the less than or equal to operator, e.g. if(goal <= 100)
     
    knewedge, Oct 27, 2012 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    knewlege is right that you should condense some of the questions to not need 'or', however you're entire statement is filled with redundancies and oddities.

    Think about what you are asking here:

    if ((goal ==100 || goal > 100) || (goal < 0 || goal === 0))

    which is the same as

    if ((goal >=100) || (goal <== 0)) {

    what follows doesn't make any sense.

    if( goal > 70 ) // 70..99 would never EVER actually run.

    else if( goal > 60 ) // 60..69 would never actually run

    etc, etc...

    I think you either want to test that in reverse, or make the 'invalid' response the FIRST action, not the last. Likewise rather than ===, I'd suggest using isNumeric to test if the input is a number instead. Also, where possible avoid saying the same thing over and over and over again...

    What I THINK you are trying to do, if I'm reading this correctly:
    
    if ((!IsNumeric(goal)) || (goal > 100) || (goal < 0)) {
    	alert('Invalid goal, outside range 0-100');
    } else {
    	if (goal > 70) {
    		grade = 'First Class';
    	} else if (goal > 60) {
    		grade = '2.1';
    	} else if (goal > 50) {
    		grade = '2.2';
    	} else if (goal > 40) {
    		grade = 'Third Class';
    	} else grade = 'Fail';
    	alert('Goal: ' + goal + '% - Grade: ' + grade);
    }
    
    Code (markup):
    Though I'm guessing wildly on that. Wondering also if those should be >= instead of > inside the logic -- is a 70 first-class or 2.1? with > on those if statements it would be outputting 2.1... same with a 40 being a fail instead of third class... if those are supposed to be inclusive, I'd use:

    
    if ((!IsNumeric(goal)) || (goal > 100) || (goal < 0)) {
    	alert('Invalid goal, outside range 0-100');
    } else {
    	if (goal >= 70) {
    		grade = 'First Class';
    	} else if (goal >= 60) {
    		grade = '2.1';
    	} else if (goal >= 50) {
    		grade = '2.2';
    	} else if (goal >= 40) {
    		grade = 'Third Class';
    	} else grade = 'Fail';
    	alert('Goal: ' + goal + '% - Grade: ' + grade);
    }
    
    Code (markup):
     
    deathshadow, Nov 1, 2012 IP