admin管理员组

文章数量:1022989

Write a function minimum3 that returns the smallest number of 3 floating-point numbers. use the math.min function. Incorporate into a script that reads 3 values from user and determines the smallest.

<!DOCTYPE html>


<html>
<head>
<title>
Javascript Program to find smallest from 3 numbers</title>

<script type="text/javascript">


var num1;
var num2;
var num3;
var num1Int = parseInt(num1);
var num2Int = parseInt(num2);
var num3Int = parseInt(num3);
var num4 = Math.min(num1Int, num2Int, num3Int);


document.write(num4);

</script>
</head>
<body>

<form name=f1>
num1: <input type="textbox" name=num1><br>
num2: <input type="textbox" name=num2><br>
num3: <input type="textbox" name=num3><br><br><br>

<button onclick="num4">Smallest</button>
</form>
</body>
</html>

Write a function minimum3 that returns the smallest number of 3 floating-point numbers. use the math.min function. Incorporate into a script that reads 3 values from user and determines the smallest.

<!DOCTYPE html>


<html>
<head>
<title>
Javascript Program to find smallest from 3 numbers</title>

<script type="text/javascript">


var num1;
var num2;
var num3;
var num1Int = parseInt(num1);
var num2Int = parseInt(num2);
var num3Int = parseInt(num3);
var num4 = Math.min(num1Int, num2Int, num3Int);


document.write(num4);

</script>
</head>
<body>

<form name=f1>
num1: <input type="textbox" name=num1><br>
num2: <input type="textbox" name=num2><br>
num3: <input type="textbox" name=num3><br><br><br>

<button onclick="num4">Smallest</button>
</form>
</body>
</html>
Share Improve this question edited Nov 15, 2016 at 6:34 Geeky Ninja 6,0468 gold badges42 silver badges54 bronze badges asked Nov 15, 2016 at 5:50 Ray KeownRay Keown 91 gold badge1 silver badge2 bronze badges 1
  • 1 Using parseInt seems counter to this requirement ~ "Write a function minimum3 that returns the smallest number of 3 floating-point numbers" – Phil Commented Nov 15, 2016 at 5:53
Add a ment  | 

8 Answers 8

Reset to default 2

Write a function...

To start, you need to place the statements you wrote inside the body of a function that takes 3 floating point numbers. So, start by writing a simple function:

function minimum3(num1, num2, num3) {
    return Math.min(num1, num2, num3);
}

Now you need to get the field values, parse the floating point numbers from them (for this you should use parseFloat, not parseInt) and pass them to your function. I would remend that you do this prior to calling your function rather than doing it within your function body; this will keep your function generic enough to use again should you need to find the minimum of 3 numbers (you probably won't, but it's good practice):

// get the field values
var val1 = document.querySelector('[name="num1"]').value;
var val2 = document.querySelector('[name="num2"]').value;
var val3 = document.querySelector('[name="num3"]').value;
// parse the floating point numbers
var number1 = parseFloat(val1);
var number2 = parseFloat(val2);
var number3 = parseFloat(val3);
// pass them to your function
var minimum = minimum3(number1, number2, number3);

Math.min(a, b) returns a when a <= b and b otherwise.

In order to find the minimum of n numbers, you can organize it as a bracket tournament. For example, to find the minimum of the following 20 numbers,

[11, 8, 53, 46, 34, 55, 85, 12, 34, 87, 890, 22, 63, 12, 76, 8654, 345, 8, 9, 23]

you can start pairing them off and get the minimum of each pair.

[11,     8] ->  8
[53,    46] -> 46
[34,    55] -> 34
[85,    12] -> 12
[34,    87] -> 34
[890,   22] -> 22
[63,    12] -> 12
[76,  8654] -> 76
[345,    8] ->  8
[9,     23] ->  9

Now you have 10 numbers left, from which you can make 5 pairs to get 5 of the smallest 10 numbers. You can repeat this process until you have only two "candidate" pairs left, which necessarily includes the smallest number of the original set.

Hopefully this puts you in the right mindset to solve this problem.

Why limit it to only three known inputs when you could use any number...

const form = document.querySelector('form')
const inputs = form.querySelectorAll('input[type="number"]')
const out = document.querySelector('output')

form.addEventListener('input', e => {
  out.value = Math.min(...Array.prototype.map.call(inputs, input => input.value))
}, false)
<form>
  <p><input type="number"></p>
  <p><input type="number"></p>
  <p><input type="number"></p>
  <p>Min = <output name="min"></output></p>
</form>

Try following script:

<form name=f1>
   num1: <input type="text" name=num1 id="txtNum1"><br>
   num2: <input type="text" name=num2 id="txtNum2"><br>
   num3: <input type="text" name=num3 id="txtNum3"><br><br><br>

   <button onclick="Calculatenum4();">Smallest</button>
</form>

 <script type="text/javascript"> 
    function Calculatenum4() {
        var num1 = document.getElementById('txtNum1');
        var num2 = document.getElementById('txtNum2');
        var num3 = document.getElementById('txtNum3');

        var num4 = Math.min(parseFloat(num1.value),  
           parseFloat(num2.value), parseFloat(num3.value));

        alert(num4);
    }
</script>
<!DOCTYPE html>
<html>
<body>

<p>Find minimum:</p>

<input id="numb1">
<input id="numb2">
<input id="numb3">

<button type="button" onclick="myFunction()">Submit</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x,y,z,text;


x = document.getElementById("numb1").value;
y=document.getElementById("numb2").value;
z=document.getElementById("numb3").value;


    if (isNaN(x)) {
        text = "Input not valid";
    } else {
        text = Math.min(x,y,z)
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>

Here is possible solution for you. First, you won't get anything on passing variable in onclick event, you need to pass method/function, like getNum(); second, you need to prevent form from proceeding submission, by returning false. After that, you need to take the values from inputs. The way you did it, you will need to use document.getElementsByName, but since it would return an array, you would need to retrieve at least first index [0]. After that, the Math.min method pares between 2 possible values, so you would need first to pare first 2 values, then result from first two, and third value. There is a more plex way to do this with multiple values parison, but this is better way to help you understand. After that, you cannot write to whole document results, but you need some container, or to output it in console using console.log. In this case, you need something visible, so we use container with "results" ID, and document.getElementByID() method and innerHTML to write the output for results.

  <!DOCTYPE html>    
    <html>
    <head>
    <title>
    Javascript Program to find smallest from 3 numbers</title>

    <script type="text/javascript">

    function getMin(){
    var num1 = document.getElementsByName('num1')[0].value;
    var num2 = document.getElementsByName('num2')[0].value;
    var num3 = document.getElementsByName('num3')[0].value;
    console.log(num1, num2, num3);
    var num1Int = parseInt(num1);
    var num2Int = parseInt(num2);
    var num3Int = parseInt(num3);
    var num4 = Math.min(num1Int, num2Int);
        num4 = Math.min(num4, num3);



    document.getElementById('results').innerHTML = num4;    

    return false;
    }


    </script>
    </head>
    <body>

    <form name=f1 onsubmit="return false;">
    num1: <input type="textbox" name=num1><br>
    num2: <input type="textbox" name=num2><br>
    num3: <input type="textbox" name=num3><br><br><br>

    <button onclick="getMin()">Smallest</button>
    </form>
    <div id="results"></div>
    </body>
    </html>

Change the input type="number" and apply with id of each input.And the get the value from id declare as variable. Finally passing to Math.min() function.

If type=number no need parseInt.else is a type=text use parseInt .input types

Change the onclick onlclick =num4() instead of onclick="num4" its function not a string.

<!DOCTYPE html>
<html>
<body>
<form name=f1>
num1: <input type="number" id="num1" name=num1><br>
num2: <input type="number" id="num2" name=num2><br>
num3: <input type="number"id="num3" name=num3><br><br><br>

<button onclick="num4()">Smallest</button>
</form>
  <script>
    function num4(){
var num1 = document.getElementById('num1').value;
var num2 =document.getElementById('num2').value;
var num3 =document.getElementById('num3').value;
var num4 = Math.min(num1, num2, num3);
  console.log(num4);
} 
  </script>
  </body>
  </html>

You can write a function like this and return the result:

function minimum3(){
       var num1 = document.getElementsByName('num1').value
       var num2 = document.getElementByName('num2').value;
       var num3 = document.getElementByName('num3').value;

       var num4 = Math.min(num1,num2,num3);
       return num4;
}

then change this the following on your code:

<button onclick="minimum3()">Smallest</button>

Write a function minimum3 that returns the smallest number of 3 floating-point numbers. use the math.min function. Incorporate into a script that reads 3 values from user and determines the smallest.

<!DOCTYPE html>


<html>
<head>
<title>
Javascript Program to find smallest from 3 numbers</title>

<script type="text/javascript">


var num1;
var num2;
var num3;
var num1Int = parseInt(num1);
var num2Int = parseInt(num2);
var num3Int = parseInt(num3);
var num4 = Math.min(num1Int, num2Int, num3Int);


document.write(num4);

</script>
</head>
<body>

<form name=f1>
num1: <input type="textbox" name=num1><br>
num2: <input type="textbox" name=num2><br>
num3: <input type="textbox" name=num3><br><br><br>

<button onclick="num4">Smallest</button>
</form>
</body>
</html>

Write a function minimum3 that returns the smallest number of 3 floating-point numbers. use the math.min function. Incorporate into a script that reads 3 values from user and determines the smallest.

<!DOCTYPE html>


<html>
<head>
<title>
Javascript Program to find smallest from 3 numbers</title>

<script type="text/javascript">


var num1;
var num2;
var num3;
var num1Int = parseInt(num1);
var num2Int = parseInt(num2);
var num3Int = parseInt(num3);
var num4 = Math.min(num1Int, num2Int, num3Int);


document.write(num4);

</script>
</head>
<body>

<form name=f1>
num1: <input type="textbox" name=num1><br>
num2: <input type="textbox" name=num2><br>
num3: <input type="textbox" name=num3><br><br><br>

<button onclick="num4">Smallest</button>
</form>
</body>
</html>
Share Improve this question edited Nov 15, 2016 at 6:34 Geeky Ninja 6,0468 gold badges42 silver badges54 bronze badges asked Nov 15, 2016 at 5:50 Ray KeownRay Keown 91 gold badge1 silver badge2 bronze badges 1
  • 1 Using parseInt seems counter to this requirement ~ "Write a function minimum3 that returns the smallest number of 3 floating-point numbers" – Phil Commented Nov 15, 2016 at 5:53
Add a ment  | 

8 Answers 8

Reset to default 2

Write a function...

To start, you need to place the statements you wrote inside the body of a function that takes 3 floating point numbers. So, start by writing a simple function:

function minimum3(num1, num2, num3) {
    return Math.min(num1, num2, num3);
}

Now you need to get the field values, parse the floating point numbers from them (for this you should use parseFloat, not parseInt) and pass them to your function. I would remend that you do this prior to calling your function rather than doing it within your function body; this will keep your function generic enough to use again should you need to find the minimum of 3 numbers (you probably won't, but it's good practice):

// get the field values
var val1 = document.querySelector('[name="num1"]').value;
var val2 = document.querySelector('[name="num2"]').value;
var val3 = document.querySelector('[name="num3"]').value;
// parse the floating point numbers
var number1 = parseFloat(val1);
var number2 = parseFloat(val2);
var number3 = parseFloat(val3);
// pass them to your function
var minimum = minimum3(number1, number2, number3);

Math.min(a, b) returns a when a <= b and b otherwise.

In order to find the minimum of n numbers, you can organize it as a bracket tournament. For example, to find the minimum of the following 20 numbers,

[11, 8, 53, 46, 34, 55, 85, 12, 34, 87, 890, 22, 63, 12, 76, 8654, 345, 8, 9, 23]

you can start pairing them off and get the minimum of each pair.

[11,     8] ->  8
[53,    46] -> 46
[34,    55] -> 34
[85,    12] -> 12
[34,    87] -> 34
[890,   22] -> 22
[63,    12] -> 12
[76,  8654] -> 76
[345,    8] ->  8
[9,     23] ->  9

Now you have 10 numbers left, from which you can make 5 pairs to get 5 of the smallest 10 numbers. You can repeat this process until you have only two "candidate" pairs left, which necessarily includes the smallest number of the original set.

Hopefully this puts you in the right mindset to solve this problem.

Why limit it to only three known inputs when you could use any number...

const form = document.querySelector('form')
const inputs = form.querySelectorAll('input[type="number"]')
const out = document.querySelector('output')

form.addEventListener('input', e => {
  out.value = Math.min(...Array.prototype.map.call(inputs, input => input.value))
}, false)
<form>
  <p><input type="number"></p>
  <p><input type="number"></p>
  <p><input type="number"></p>
  <p>Min = <output name="min"></output></p>
</form>

Try following script:

<form name=f1>
   num1: <input type="text" name=num1 id="txtNum1"><br>
   num2: <input type="text" name=num2 id="txtNum2"><br>
   num3: <input type="text" name=num3 id="txtNum3"><br><br><br>

   <button onclick="Calculatenum4();">Smallest</button>
</form>

 <script type="text/javascript"> 
    function Calculatenum4() {
        var num1 = document.getElementById('txtNum1');
        var num2 = document.getElementById('txtNum2');
        var num3 = document.getElementById('txtNum3');

        var num4 = Math.min(parseFloat(num1.value),  
           parseFloat(num2.value), parseFloat(num3.value));

        alert(num4);
    }
</script>
<!DOCTYPE html>
<html>
<body>

<p>Find minimum:</p>

<input id="numb1">
<input id="numb2">
<input id="numb3">

<button type="button" onclick="myFunction()">Submit</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x,y,z,text;


x = document.getElementById("numb1").value;
y=document.getElementById("numb2").value;
z=document.getElementById("numb3").value;


    if (isNaN(x)) {
        text = "Input not valid";
    } else {
        text = Math.min(x,y,z)
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>

Here is possible solution for you. First, you won't get anything on passing variable in onclick event, you need to pass method/function, like getNum(); second, you need to prevent form from proceeding submission, by returning false. After that, you need to take the values from inputs. The way you did it, you will need to use document.getElementsByName, but since it would return an array, you would need to retrieve at least first index [0]. After that, the Math.min method pares between 2 possible values, so you would need first to pare first 2 values, then result from first two, and third value. There is a more plex way to do this with multiple values parison, but this is better way to help you understand. After that, you cannot write to whole document results, but you need some container, or to output it in console using console.log. In this case, you need something visible, so we use container with "results" ID, and document.getElementByID() method and innerHTML to write the output for results.

  <!DOCTYPE html>    
    <html>
    <head>
    <title>
    Javascript Program to find smallest from 3 numbers</title>

    <script type="text/javascript">

    function getMin(){
    var num1 = document.getElementsByName('num1')[0].value;
    var num2 = document.getElementsByName('num2')[0].value;
    var num3 = document.getElementsByName('num3')[0].value;
    console.log(num1, num2, num3);
    var num1Int = parseInt(num1);
    var num2Int = parseInt(num2);
    var num3Int = parseInt(num3);
    var num4 = Math.min(num1Int, num2Int);
        num4 = Math.min(num4, num3);



    document.getElementById('results').innerHTML = num4;    

    return false;
    }


    </script>
    </head>
    <body>

    <form name=f1 onsubmit="return false;">
    num1: <input type="textbox" name=num1><br>
    num2: <input type="textbox" name=num2><br>
    num3: <input type="textbox" name=num3><br><br><br>

    <button onclick="getMin()">Smallest</button>
    </form>
    <div id="results"></div>
    </body>
    </html>

Change the input type="number" and apply with id of each input.And the get the value from id declare as variable. Finally passing to Math.min() function.

If type=number no need parseInt.else is a type=text use parseInt .input types

Change the onclick onlclick =num4() instead of onclick="num4" its function not a string.

<!DOCTYPE html>
<html>
<body>
<form name=f1>
num1: <input type="number" id="num1" name=num1><br>
num2: <input type="number" id="num2" name=num2><br>
num3: <input type="number"id="num3" name=num3><br><br><br>

<button onclick="num4()">Smallest</button>
</form>
  <script>
    function num4(){
var num1 = document.getElementById('num1').value;
var num2 =document.getElementById('num2').value;
var num3 =document.getElementById('num3').value;
var num4 = Math.min(num1, num2, num3);
  console.log(num4);
} 
  </script>
  </body>
  </html>

You can write a function like this and return the result:

function minimum3(){
       var num1 = document.getElementsByName('num1').value
       var num2 = document.getElementByName('num2').value;
       var num3 = document.getElementByName('num3').value;

       var num4 = Math.min(num1,num2,num3);
       return num4;
}

then change this the following on your code:

<button onclick="minimum3()">Smallest</button>

本文标签: