SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: if statment with with a condition of 4 or not working

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if statment with with a condition of 4 or not working

    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

    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" );
    }

  2. #2
    SitePoint Mentor bronze trophy
    chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,041
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)
    The current way your code is setup won't work as your if statement is always expecting to see a value less than 0 or greater than 100, see the below which is the same code above but more simplified and work from my tests.

    Code JavaScript:
    // Ex: 3-1 Grading Work
    var goal  = prompt('Please input the goal(%)'),
        goal  = parseInt(goal, 10),
        grade = 'Not Yet Graded';
     
    if (goal >= 0 && goal <= 100) {
        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);
    } else {
        alert('Invalid goal, outside range 0-100');
    }
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,454
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    Hi,

    Your problem is in first if statement where you are checking if goal
    1. is equal to one hundred, or
    2. is greater than 100, or
    3. is less than zero, or
    4. is equal to zero


    As for example, 1000 is greater than 100 (check 2.) it lets this number through.
    It then checks if 1000 is greater than 70 (which it is) and then alerts that you have a first class grade.

    It would be better to write this first if statement as:
    Code JavaScript:
    if (goal >= 0 && goal <= 100)
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  4. #4
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys thanks so simple was pulling my hair out, thanks alot guys

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •