What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

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?
 
Okay, great to know. Would you like me to rewrite your code to solve the issue you're having?
 
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.
 
These are index and script files.
 

Attachments

  • index.JPG
    index.JPG
    47.9 KB · Views: 3
  • script.JPG
    script.JPG
    36.9 KB · Views: 3
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?
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back