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.