dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Tryin' to do something

  1. #1
    Join Date
    Jun 2017
    Posts
    1

    Tryin' to do something

    Hi, i need your help. I'm trying to do something like this

    Code:
    <script>
    
     function func() 
            {
                var input = document.getElementById('num').value;
                var output = document.getElementById('para');
            
                var a = 60;
                
                if(input <= 10) {
                    inputx = a;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 10 && input <= 20) {
                    inputx = a * 2;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 20 && input <= 30) {
                    inputx = a * 3;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 30 && input <= 40) {
                    inputx = a * 4;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 40 && input <= 50) {
                    inputx = a * 5;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 50 && input <= 60) {
                    inputx = a * 6;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 60 && input <= 70) {
                    inputx = a * 7;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 70 && input <= 80) {
                    inputx = a * 8;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 80 && input <= 90) {
                    inputx = a * 9;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
                
                else if(input > 90 && input <= 100) {
                    inputx = a * 10;
                    output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
                }
        </script>
       
    <input type="text" placeholder="enter the number" id="num">
        <button onClick="func()">Calculate</button>
        <p id="para"></p>
       
    </body>
    </html>
    Let me explain. When a user enter some number in the input field and press the calculate button, according some system like this
    https://image.prntscr.com/image/sTFm...setCg_ThgA.png
    He get an answer on that output.
    If he enter 100, the output should be 600 (60 * 10 because it's between 90 - 100, >90 but <= 100 )
    if he enter 150, the output should be 900 (60 * 15)
    if he enter 109, the output should be 660 (60 * 11 because it's between 100 - 110)
    if he enter 111, the output should be 720 (60 * 12 because it's between 110-120)
    My code it is working but isn't best pratice. I must write thousands of lines for each case if user enters a big value. So, i need help doing this in a few lines. Sorry my incredible bad English, i'm such a noob.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,648
    The common multiplier seemed to be related to the upper test limit in the conditionals you provides which were to be frank, bloated with repetitive elements that can be rewritten to produce something like below.
    HTML Code:
    Number.prototype.range =function(a,b){
    	var multiplier = b / 10;
    	if( this > a && this <=b ) return multiplier;
    	return 0;
    }
    
    function score( fm ) {
    	var input = fm.num.value-0,         
    			inputx = 60;
    	if( !input ) return;
    
    	for(chk=0; chk<300; chk+=10){
    		if( (i=input.range(chk,chk+10) ) ){
    			inputx *= i;
    			break;
    		}
    	}
    	document.getElementById('para').innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.";
     }
    </script>
    </head>
    <body>
    <div id="para"><div>
    <form name="scores" action="javascript:;" onsubmit="score( this ); return false;">
    	<input name="num" type="text" placeholder="number">
    	<input name="Submit" type="submit" value="Get Score" >
    </form>
    To go to higher scores, modify the for(chk=0; chk<300; chk+=10) and change the chk<300 to whatever number you want to go up to.

    I tried the routine out and it ouputs what you require based upon what you have asked for help with.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,805

    Lightbulb

    If I understand the problem correctly, this "funkyConvert" function
    should do what you want by sending the user input to it as a parameter.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> HTML5 page </title>
    </head>
    <body>
    <table id="tbl" border="1">
    </table>
    <script>
    // For: http://www.webdeveloper.com/forum/showthread.php?364327-Tryin-to-do-something
    
    function funkyConvert(N) {
      var v = (parseInt(N / 10) * 60)+60;
      return v;
    }
    function init() {
      var str = '';
      for (var i=0; i<600; i = i+25) {
        str += '<tr><td>'+i+'</td><td>'+funkyConvert(i)+'</td></tr>';
      }
      document.getElementById('tbl').innerHTML = str;
    } init();
    </script>
    
    </body>
    </html>
    Simply get%
    Last edited by JMRKER; 06-19-2017 at 12:30 PM.

  4. #4
    Join Date
    Jun 2017
    Posts
    12
    Here is my solution. There are no limits to what number the user can input.


    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> Calculate EXP </title>
    </head>
    <body>
    
    <script>
    
    function calculate() {
            var input = document.getElementById('num').value;
            var output = document.getElementById('para');
            var a = 60;
    	if( !input ) return;
    	
    	var inputx = Math.ceil((input)/10)*a;
    
    	document.getElementById('para').innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.";
     }
    </script>
    </head>
    <body>
    <input type="text" placeholder="enter the number" id="num">
        <button onClick="calculate()">Calculate</button>
        <p id="para"></p>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles