0

I have tried two different Javascript code to find the largest number from three user inputs. I am not getting the highest number from none of the codes. Please help!

HTML Code:

    Assignment 1: <input type="text" name="num1"><br>
    Assignment 2: <input type="text" name="num2"><br>
    Assignment 2: <input type="text" name="num3"><br>

    <input type="button" id="high" value="high" onclick="high()">
    <input type="text" id="avg">

    <p id="result"></p>

JavaScript Code(Try 1):

function high() {
        let num1 = document.getElementsByName("num1")[0].value;
        let num2 = document.getElementsByName("num2")[0].value;
        let num3 = document.getElementsByName("num3")[0].value;

        var avg = Math.max(Number(num1),Number(num2),Number(num3))

        document.getElementById("result").innerHTML=avg;

JavaScript Code(Try 2):

function high() {
    let num1 = document.getElementsByName("num1")[0].value;
    let num2 = document.getElementsByName("num2")[0].value;
    let num3 = document.getElementsByName("num3")[0].value;

  if(Number(num1)>Number(num2) && Number(num1)>Number(num3))
    {
        document.getElementsByName("avg")[0].value = num1;
    }

   if(Number(num2)>Number(num1) && Number(num2)>Number(num3))
    {
        document.getElementsByName("avg")[0].value = num2;
    }

   if(Number(num3)>Number(num2) && Number(num3)>Number(num1))
    {
        document.getElementsByName("avg")[0].value = num3;
    }
3
  • Tried your first function and it worked, although you are writing avg every now and then, which is short for "average", not "highest". Commented May 22, 2020 at 1:39
  • 1
    Please edit your question to include the HTML from your answer and delete your answer. Answers should, well, answer the question. Commented May 22, 2020 at 1:46
  • Also, please edit your question and explain what is not working about the attempts you have made. Commented May 22, 2020 at 1:48

1 Answer 1

2

you are making things complex try this code .

Assignment 1: <input type="text" name="num1"><br>
Assignment 2: <input type="text" name="num2"><br>
Assignment 2: <input type="text" name="num3"><br>

<button onclick="high()">Submit</button>

<p id="result"></p>

<script>

    function high(){
            var arr = document.querySelectorAll("input");
            var emptyarray = [];
            arr.forEach((elem) => {
                emptyarray.push(elem.value);
            })
            var max = Math.max.apply(null, emptyarray);
            document.getElementById("result").innerHTML=max;
    }
</script>

you will get the heighest number result , you can restrict the input field to just enter the number so that no one can add string instead of number and your code work fine .

Sign up to request clarification or add additional context in comments.

1 Comment

great :) if you this answer useful then you can mark this as answered so other people can see the answer as well .. thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.