Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Why can't I retrieve numbers from second and third input?

I can't retrieve numbers from two inputs. First input #inpArraysNumber works fine. Second and third doesn't. I don't know what I am doing wrong. Could you please tell me how to solve this problem?

// this doesn't work
let min = document.getElementById("inpMinValue").value;
let max = document.getElementById("inpMaxValue").value;

// this works
let N = document.getElementById("inpArraysNumber").value;

JS

const btnClick = document.getElementById("btnClick");
var buttonFlag = false;
btnClick.addEventListener("click", onClick);

function onClick(){
    
    // one click
    if (buttonFlag){
        console.log(buttonFlag);
        return;
    }
    buttonFlag = true;
     
    const nums = [];
    let N = document.getElementById("inpArraysNumber").value;
    
    for(let i=0; i<N; i++){
        nums[i] = [];
    }
    const uniqueNumber = function (minValue, maxValue, maxSubarrayIndex) {
        const number = Math.floor(Math.random() * (maxValue - minValue + 1) + minValue);
        const subarrays = nums.slice(0, maxSubarrayIndex + 1);
        if (subarrays.every(subarr => !subarr.includes(number))) {
            nums[maxSubarrayIndex].push(number);
        } else {
            uniqueNumber(minValue, maxValue, maxSubarrayIndex);
        }
    }
    // this doesn't work
    let min = document.getElementById("inpMinValue").value;
    let max = document.getElementById("inpMaxValue").value;

    // this works fine
    // let min = 55;
    // let max = 155;

    const howMany = parseInt((max-min)/N);
    for(let i=0; i<N; i++){
        for(let j=0; j<howMany; j++){
            uniqueNumber(min, max, i);
        }
    }
 // here more code, but I couldn't post it because of "your post is mostly code"
}

HTML


<body>
    <div id="numbers">
        <h1>Unique numbers in Arrays</h1>
        <div id="inputsDiv">
            <input type="number" id="inpArraysNumber" placeholder="Arrays number">
            <input type="number" id="inpMinValue" placeholder="Min value">
            <input type="number" id="inpMaxValue" placeholder="Max value">
            <button id="btnClick">click</button>
        </div>
        <div id="innerNumbers">
        </div>
    </div>
    <script src="./js/script.js"></script>
</body>

When I set min and max variables in my code everything works fine.

setting theese two variables works fine:

let min = 55;

let max = 155;

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!