JavaScript Compare 3 values within certain percentage of each other compare values within list python,compare values within dictionary python,

I am trying to make this:

  1. 3 input boxes
  2. take the value of the input and compare to a certain percentage

if values are within 3% of each other. output = something else output something else.

any help would be appreciated on getting started.

Answer:1

First create the inputs which on a basic level looks like this:

<input type="text" width="3em">

That will create a single input tag. So, placing three, or however many, will create the inputs. Now, we need a way to get the inputs we just created. To do this we'll need some JavaScript.

document.getElementsByTagName("input");

This will get us all input tags on the screen. Now, we can iterator through these tags and get the data we need in which I will place into an array.

Note: Each input tag has a property called value which stores the string placed inside of the input which can be changed to a number by placing a + in front.

var numbers = [],
    inputs  = document.getElementsByTagName("input");

for(var i = 0; i < inputs.length; ++i)
{
    var number = +inputs[i].value;
    // Makes sure that a number is there.
    if(!isNaN(number))
        numbers.push(number);
}

Now that we have everything in an array, time for some math!


Let us look at an easy example of comparing the percentage off a particular number is from another. If we are given 7 and 3 how far off are they from each other? First we find the average -> (7 + 3) / 2 then we find the difference between |3 - 7| then we divide the difference by the average ... 4 / 5 = .8. Yay! But, what about three numbers? Well, now we must do that between every number... Then we check each one to see if within range. Yuck! To do this it will require an embedded for loop.

So, we need to have an embedded for loop such that we do not repeat comparisons. Just like the following:

numbers => 1 2 3 4

Compare 1 -> 2, 1 -> 3, 1 -> 4

Compare 2 -> 3, 2 -> 4

Compare 3 -> 4

Stop!

This brought me to this mess:

var inrange   = true,
    tolerance = 3/*In percentage!*/;
for(var i = 0; i < numbers.length && inrange; ++i)
    for(var j = i + 1; j < numbers.length && inrange; ++j)
    {
        var diff = (numbers[i] - numbers[j]),
        // Need to make sure diff is positive. Could use Math.abs...
        pdiff    = 2 * (diff < 0 ? -1*diff : diff) 
                 / (numbers[i] + numbers[j]);
        // If false will cause the loops to stop.
        inrange = (100 * pdiff) <= tolerance;
    }

With the calculation function we can now focus on the display!


Now, we need a button that will call to a function and do the magic for us! This can be done with the input tag as well, but by changing its type.

<input type="submit" value="calculate" onclick="calculate()">

So, now we need a way to display... A really easy way is to have a <p> tag with an id.

<p id="display"></p>

To get this tag, we can use some JavaScript as well.

document.getElementById("display");

These tags have an attribute called innerHTML in which we can edit to do, well..., change the inner HTML.

var p = document.getElementById("display");
p.innerHTML = "Hello World!";

Now, we can combine everything above to get:

<input type="text" width="3em">
<input type="text" width="3em">
<input type="text" width="3em">
<input type="submit" value="calculate" onclick="calculate()">
<p id="display"></p>
<p id="results"></p>

<script>
// The tolerance for the numbers to be within.
    var tolerance = 3,
    // The number of inputs expected.
        amount    = 3;

    function calculate() {
        var numbers = [],
            inputs  = document.getElementsByTagName("input");
        
        // Gets all of the numbers.
        for(var i = 0; i < inputs.length; ++i) 
        // Need to check type to make sure not getting the button.
            if(inputs[i].type === "text"){
            var number = +inputs[i].value;
            if(!isNaN(number))
                numbers.push(number);
        }

        // Need to make sure the correct amount of inputs were found.
        if(numbers.length !== amount) return;

        var inrange = true,
        // Gets the results p tag.
            results  = document.getElementById("results");
        // Clear old results.
        results.innerHTML = "";

        // Compares each number finding the avg percent difference.
        for(var i = 0; i < numbers.length && inrange; ++i)
            for(var j = i + 1; j < numbers.length && inrange; ++j) {
                var diff  = (numbers[i] - numbers[j]),
                    pdiff = 2 * (diff < 0 ? -1*diff : diff)
                         / (numbers[i] + numbers[j]);
                inrange = (100 * pdiff) <= tolerance;
                results.innerHTML += "Input " + (i+1) + " to " + (j+1)
                                  +  ": " + (100 * pdiff) + " " + (inrange ? "<=" : ">") + " " + tolerance
                                  +  " <br>";
            }

        // Gets the display tag.
        var p = document.getElementById("display");

        // Makes sure that within tolerance!
        if(inrange) 
            p.innerHTML = "Within tolerance!";
        else
            p.innerHTML = "Try again...";
    }
</script>
Answer:2

Here is a code sample you can derive the logic from.

https://jsfiddle.net/1u4c0jzj/

<input type="text" id="value1" />
<input type="text" id="value2" />
<input type="text" id="value3" />
<button id="checkValues">Check</button>
<div id="output" />


document.getElementById("checkValues").onclick = function(){
    var value1 = parseFloat(document.getElementById("value1").value);
    var value2 = parseFloat(document.getElementById("value2").value);
    var value3 = parseFloat(document.getElementById("value3").value);    
    var maxValue = parseFloat(Math.max(Math.max(value1, value2), value3));

    if(1-(value1 / maxValue) <= .03 && 1-(value2 / maxValue) <= .03 && 1-(value3 / maxValue) <= .03){
     alert('all numbers are within 3% of each other')   
    }
    else{
     alert('numbers NOT within 3%');   
    }
}

It takes in 3 inputs, finds the max and ensures that all 3 numbers are within 3% of the max.

Answer:3

I'm currently working on developing some set of codes to display all blobs inside specified Azure Container using web front-end. I'm expecting the final output to be something like this: I started by ...

I'm currently working on developing some set of codes to display all blobs inside specified Azure Container using web front-end. I'm expecting the final output to be something like this: I started by ...

I want to block my UI and show a spinner while it is blocked. I tried to use the following : blockUI.start("<div class='dots-loader'>Minions are Working!</div>"); // some code ...

I want to block my UI and show a spinner while it is blocked. I tried to use the following : blockUI.start("<div class='dots-loader'>Minions are Working!</div>"); // some code ...

I'm using mocha and trying to build a testing system which reports tests individually. The goal is to have traceability between tests defined in the requirements for the project and unit tests. So, ...

I'm using mocha and trying to build a testing system which reports tests individually. The goal is to have traceability between tests defined in the requirements for the project and unit tests. So, ...

  1. adding unit test to existing project c#
  2. adding unit test to existing project
  3. adding unit test to legacy code
  4. adding unit test python
  5. adding unit test in java
  6. adding unit test to c#
  7. adding unit test project
  8. qt adding unit test

I have the following jquery selector which I am trying to convert to regular javascript. $("#lelement>*").on("dblclick", function(){ }); what would the equivalent be with regular javascript ? Can ...

I have the following jquery selector which I am trying to convert to regular javascript. $("#lelement>*").on("dblclick", function(){ }); what would the equivalent be with regular javascript ? Can ...

  1. javascript equivalent jquery document ready
  2. javascript equivalent jquery find
  3. javascript equivalent jquery
  4. javascript equivalent of jquery closest
  5. javascript equivalent of jquery load
  6. javascript equivalent of jquery ready
  7. javascript equivalent of jquery append
  8. javascript equivalent of jquery trigger
  9. javascript equivalent of jquery each
  10. javascript equivalent of jquery show
  11. javascript equivalent of jquery text
  12. javascript equivalent of jquery html
  13. javascript equivalent of jquery offset
  14. javascript equivalent of jquery empty
  15. javascript equivalent of jquery animate
  16. javascript equivalent of jquery hide
  17. javascript equivalent of jquery data
  18. javascript equivalent of jquery selector
  19. javascript equivalent of jquery parents
  20. javascript equivalent of jquery click