Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2006
    Location
    Malaysia.
    Posts
    32
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Javascript conditional statement.. need help

    I need ya help .... i cant understand the question well...

    can ya help me to write the coding?

    thanks in advanced.

    here are the question.


    Write a compound conditional statement that tests whether a previously declared variable named finalScore is greater than or equal to 90, less than 90 but greater than or equal to 80, less than 80 but greater than or equal to 70, or less than 70. Do not use any logical operators (“and” or “or”). Depending on which condition is satisfied, the code should display an alert box message with an appropriate message (e.g., “Final score is in the
    80s” or “Final score is below 70”).

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    No help on writing the actual code for a school assignment. But I'll give you the tools to do it yourself:


    if..else if..else-statement:
    Code:
    if(first condition){
        statements to run if first condition is true
    }else if(second condition){
        statements to run if second condition is true
    }else{
        statements to run if all conditions are false
    }
    alert function:
    Code:
    window.alert(string to alert);
    Variable declaration:
    Code:
    var
        finalscore;
    var
        finalScore=initial value;
    Greater-than-or-equal-to operator:
    Code:
    left operand >= right operand
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Dec 2006
    Location
    Malaysia.
    Posts
    32
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Thank you ...

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Even if, usually, we do not do other's homeworks, as today I feel myself in a good mood and as I reckon it is rather a little tricky, mean and scurvy homework, here you are:

    As long as you are not allowed to use the logical operators (&& and ||), that means you have to use a sequential sorting using the conditionals if - else if.

    if(){}
    else if(){}
    else if(){}
    ....
    else{}

    and give your message a proper value each time:

    The sequntial sorting may start from each of the limits, but it is more intuitive to start if from the "lowest" level condition:
    Code:
    <script type="text/javascript">
    var finalScore=Number(prompt('Enter a final score',''));
    var message;
    if(finalScore<70){
    message='Final score is below 70';
    }
    else if(finalScore<80){
    message='Final score is in the 70s';
    }
    else if(finalScore<90){
    message='Final score is in the 80s';
    }
    else{
    message='Final score is over 90';
    }
    alert(message)
    </script>
    Welcome to the Forum!
    But, for the future, whenever you come here for some help, please, try first to solve yourself the problem, do something, even wrong, but show us that you have done all your best for that, and we might help you to understand and bypass the challenge.

    As a bonus (as the problem is to be solved in several different ways) here's a more sophisticated method, using an object, it's properties, and it's particular loop syntax. (You may surprise you teacher with that, as it looks like you are a beginner, so that if you do not understand the code, don't use it. Come again here, and we will explain it.
    Code:
    <script type="text/javascript">
    var finalScore=Number(prompt('Enter a final score',''));
    var message='Final score is over 90';
    var myObj={
    70:'Final score is below 70',
    80:'Final score is in the 70s',
    90:'Final score is in the 80s'
    }
    for(param in myObj){
    	if(finalScore<param){
    	message=myObj[param];
    	break;
    	}
    }
    alert(message)
    </script>
    Regards...
    Last edited by Kor; 12-14-2006 at 08:23 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Dec 2006
    Location
    Malaysia.
    Posts
    32
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Dear Kor...

    thanks for the coding ... it's really help me to understand... i was working on it .. and now i have to sample... so it make me easy to understand them well... thanks alot ...

  • #6
    New Coder
    Join Date
    Dec 2006
    Location
    Malaysia.
    Posts
    32
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Here are the coding which i've wrote last night, but it's didnt work much... ... did i missed out something... please help me!


    <html>
    <head>
    <title>Question 4</title>

    <script type="text/javascript">
    <!--
    var finalScore,

    finalScore=window.prompt( "Enter a final score:",'');

    if (finalScore<70)
    {
    document.writeln(finalscore.Final score is below 70);
    }
    else if(finalScore<80)
    {
    document.writeln(finalscore.Final score is in the 70s);
    }
    if (finalScore<90)
    {
    document.writeln(finalscore.Final score is in the 80s);
    }
    else{
    document.writeln(finalscore.Final score is over 90);
    }
    alert(finalscore)
    //-->
    </script>
    </head>
    <body>
    <p>Click Refresh (or Reload) to run the script again<p>
    </body>
    </html>

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    !. You have not understood quite well the conditional if(){} else if(){}... else(){}, because you skipped using else in the middle of the code. That else works like a filter. Without it, the code turns into something else. have a look what happens if you omit the else conditioner:
    Code:
    <script type="text/javascript">
    var finalScore=10;
    if(finalScore<70){
    alert('Final score is below 70')
    }
    if(finalScore<80){
    alert('Final score is in the 70s')
    }
    </script>
    The result might look wrong, but it is absolutely logical, as long as, without else, both if() conditionals act independently. The display is correct , as 10 is lower that 70 and is lower than 80 the same time

    Now, using else, we will make a connection between conditionals:
    Code:
    <script type="text/javascript">
    var x=10
    if(x<70){
    alert('Final score is below 70')
    }
    else if(x<80){
    alert('Final score is in the 70s')
    }
    </script>
    Now, else waits to see if the previous if() condition is fulfilled. If not, it will continue the comparison with the new condition. But if yes, it will bypass it's own if() condition, jumping to the next code line.


    2. write() and writeln() methods are quite obsoleted and they are not dynamic. There are several other dynamic methods to write/change text on page:

    Consider element, as the container tag which have to bear the text (the so called parentNode - <p> , <div>, <span>, <td> ... )

    The DOM standard methods:
    - element.appendChild(document.createTextNode('sometext))
    - element.firstChild.data='sometext';
    - element.firstChild.nodeValue='sometext';

    The nonstandard innerHTML method (but, even it is not standard, it is used by all the modern browsers, as it is one of the few useful IE "inventions"):
    - element.innerHTML='somethext';

    As you are beginer, we will use innerHTML, is simplier

    3. To run repeatedly a code you better nest it in a function, which is to be called by an event, following the user's action. As the user's action could be clicking on something, the event, in your case will be onclick handled in a certain element tag.

    If you need that function to be called also when the user loads the page, that that function has to be called by the onload event, handled by the window element,
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function sortFinalScore(){
    var txt;
    var finalScore=Number(window.prompt('Enter a final score'));
    if (finalScore<70){
    txt='Final score is below 70';
    }
    else if(finalScore<80){
    txt='Final score is in the 70s';
    }
    else if(finalScore<90){
    txt='Final score is in the 80s';
    }
    else{
    txt='Final score is over 90';
    }
    document.getElementById('mytext').innerHTML=txt
    }
    window.onload=sortFinalScore;
    </script>
    </head>
    <body>
    <div id="mytext">&nbsp;</div>
    <br>
    <span style="cursor:pointer;background:#E5E5E5" onclick="sortFinalScore()">Click here to run the script again</span>
    </body>
    </html>
    Last edited by Kor; 12-16-2006 at 10:42 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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