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.
CodePudding user response:
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>
CodePudding user response:
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.
CodePudding user response:
Math.floor(Math.random()* (max_number) min_number)
you are using string not index number. newRangeOfIndices is giving the value of particular index. this will work.
CodePudding user response:
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