Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda