Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Script to find the number from table and store the index into variable.

Sidddharth

New Coder
Hello Everyone,

I want to take input from user and on basis of the input i want to show the correct value of it.

For example if user enters number 5. The correct value from the below table i.e. 1 should be displayed.

Table 1
Input Value
0-5 1
6-10 2
11-15 3
16-20 4
And so on.

The table is huge upto 20000 numbers so if else is kind of lengthy. I am looking for simpler way to solve.

Ps- i am new in the coding.

Anticipating support from the community.

Thanks in advance.
 
Notice that the final value you want JS to post is the largest number in the group divided by 5.
Also look at the output of the input number divided into those largest numbers:
15/13 = 1.1538461538461537 AND 10/13 = 0.7692307692307693. The final number is always greater than 1 until we fall below the 13.
Also notice that the number we arrive at is 5 less than what we want.

Given that we do this
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0;" charset="utf-8">
</head>

<body>
    <div id="demo">Demo</div>
</body>
<script>
var a = 13;
var b = 20000;  // highest it would ever be

/* to find the instant the division yields some thing less than 1. */
while((b/a)>1){
    b=b-5;
}
b = b +5;  //bring the number to the next group
c = b/5;  //to get the outputted number

/* To show you that this works */
document.getElementById("demo").innerHTML = c;
</script>
</html>
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom