What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

How do I get the largest value?

I'd be happy to offer my help, but I am uncertain what you are trying to ask. Are you referring to an array of numbers? Where do those numbers come from?
 
Here's a solution that works for me. Please let me know if you need any other help, or if this solution doesn't work for your needs.

max-number-from-array.png
 
Can you paste the code that you are using into this thread? That way I can take a closer look at what is going on.
 
You need to change the function name for the onclick attribute on your button. Instead of saying "alert", it should read "biggestNumber()".

And try this JavaScript code out:

JavaScript:
function biggestNumber() {
 const items = [...document.querySelectorAll(".number")];
 const itemValues = [];
 for (let i = 0; i < items.length; i += 1) {
  itemValues.push(items[i].value);
 }
 console.log(Math.max(...itemValues));
}
 
function biggestNumber() { const items = [...document.querySelectorAll(".number")]; const itemValues = []; for (let i = 0; i < items.length; i += 1) { itemValues.push(items.value); } console.log(Math.max(...itemValues)); }
When I press the button, nothing happens. I use Visual Studio Code.
 
HTML:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body>
    <input type="number" class="number"> </br>
    <input type="number" class="number"> </br>
    <input type="number" class="number"> </br>
    <input type="number" class="number"> </br>
    <input type="number" class="number"> </br>
    <button id="btn" onclick="biggestNumber()">Show</button>
    <script>
      function biggestNumber() {
        const items = [...document.querySelectorAll(".number")];
        const itemValues = [];
        for (let i = 0; i < items.length; i += 1) {
          itemValues.push(items[i].value);
        }
        console.log(Math.max(...itemValues));
      }
    </script>
  </body>
</html>

Here is the exact code that I'm using for this challenge. Can you try this in your VS Code?
 
Back
Top