Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

294
Vistas
Set a custom start and end range in an array that displays a random string?

I am playing around with JavaScript while preparing for my junior developer interview.

I am trying to write a function that accepts two parameters, a beginning point and an ending point, in an array. This function should generate a random name within a custom start and end point of the array. I seemed close to getting it right, but it displays NaN. What is NaN?

Here is the code I wrote.

const names = ['Kitana', 'Liu Kang', 'Sonya Blade', 'Johnny Cage', 'Jax Briggs', 'Smoke', 'Sheeva', 'Jade']

const section = document.querySelector('section')

const para = document.createElement('p');

// Add your code here
function random(beginIndex, endIndex) {
  for (let beginIndex = 0; beginIndex < names.length; beginIndex = beginIndex + endIndex) {

    let newRangeOfIndices = names[beginIndex]

    const randomName = Math.floor(Math.random() * newRangeOfIndices)

    para.textContent = randomName
  }
}

random(2, 5)

// Don't edit the code below here!

section.innerHTML = ' ';

section.appendChild(para);
<section></section>

You will notice that I already set a custom limit in the function to be run, 2 to 5. But it's still not working. Please help me out.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You don't for loop to get a random number.

To get a random index.

let randomIndex = Math.floor(Math.random() * (endIndex - beginIndex + 1) + beginIndex)

Then get the random name from the names array.

let randomName = names[randomIndex]

const names = ['Kitana', 'Liu Kang', 'Sonya Blade', 'Johnny Cage', 'Jax Briggs', 'Smoke', 'Sheeva', 'Jade']

const section = document.querySelector('section')

const para = document.createElement('p');

// Add your code here
function random(beginIndex, endIndex) {
    let randomIndex = Math.floor(Math.random() * (endIndex - beginIndex + 1) + beginIndex)  
    let randomName = names[randomIndex]

    para.textContent = randomName
}

random(2, 5)

// Don't edit the code below here!

section.innerHTML = ' ';

section.appendChild(para);
<section></section>

about 4 years ago · Juan Pablo Isaza Denunciar

0

The author asked two questions here... the first one related to the code and here is my solution:

const names = ['Kitana', 'Liu Kang', 'Sonya Blade', 'Johnny Cage', 'Jax Briggs', 'Smoke', 'Sheeva', 'Jade'];

// create a generic randomName function that takes 3 parameters beginIndex, endIndex and an array which returns a random positioned value within the range
function randomName(beginIndex, endIndex, arr) {
  const randomNumber = Math.floor(Math.random() * (endIndex - beginIndex + 1) + beginIndex);
    return arr[randomNumber];
}

// call the function with the proper argument
randomName(2, 5, names);

the second question is What is NaN?

Answer: as stated by MDN Docs

The global NaN property is a value representing Not-A-Number.

When calculating numbers but sending a string value to parse, the JavaScript parser is throwing an error that it is Not-A-Number.

let's see we have two variables

let x = 10;
let y = 'hello';
    
console.log(typeof x); // number
console.log(typeof y); // string

// try to multiplied x by y
console.log(x * y);    // NaN

In Your case:

function random(beginIndex, endIndex) {
  for (let beginIndex = 0; beginIndex < names.length; beginIndex = beginIndex + endIndex) {

    // this will be a string that extracts the value of the beginIndex position value from the names array.
    let newRangeOfIndices = names[beginIndex]

    // here you try to multiply the Random Number by a string and getting NaN
    const randomName = Math.floor(Math.random() * newRangeOfIndices)

    para.textContent = randomName
  }
}

random(2, 5)

Best wishes for your interview

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use:

return Math.random() * (max - min) + min;

To get a number as min-max. Then, use the random number output as an index in the array. Always avoid for loops in situations like this - there is most likely always another way to do it.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda