Build a Sorting Visualizer - Failed: 4. Your generateArray function should make use of the generateElement function

Tell us what’s happening:

i succeed return an array containing five integers between 1 - 100
and i made use for generate Element function but i can’t pass the two test cases

  1. Your generateArray function should make use of the generateElement function.
    Failed:5. Your generateArray function should return an array containing five random integers between 1 and 100.

Your code so far

/* file: script.js */
const generateElement = () => {
  let arr = []
  for(let i = 1; i <= 100; i++) {
    arr.push(i)
    // break
  }
  return arr[Math.floor(Math.random() * arr.length)]          
}

const generateArray = (num) => {
  let stash = []
  if(num < 1) {
    return
  }
    stash.push(generateElement())
    stash.push(generateElement())
    stash.push(generateElement())
    stash.push(generateElement())
    stash.push(generateElement())
     generateArray(num - 1)
    return stash  

}
    
console.log(generateArray(generateElement()))
      

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer

Please explain in your own word what you think this bit of code is doing.

it pushes random numbers between 1 - 100 till i get 5 elements in the array

i think i made a bad practice here confusing between recursion lessons and functional programming section !!

2 Likes

agree.

also, why are you sending in a random number when you are getting a random number for each push statement?

Take it one step at a time.

  1. You should have a function named generateElement that returns a random integer between 1 and 100, inclusive.

Does this function return one random number?

const generateElement = () => {
  let arr = []
  for(let i = 1; i <= 100; i++) {
    arr.push(i)
    // break
  }
  return arr[Math.floor(Math.random() * arr.length)]          
}

It seems to return an array?

no it returns an index of the array which is random number between 1 - 100 as u see, i logged it to the console and i see it

Ah, haha ok I see what it is now. :+1: My mistake

Create the array :+1:

let stash = []

These lines push 5 random integers onto the array :+1:

stash.push(generateElement())
    stash.push(generateElement())
    stash.push(generateElement())
    stash.push(generateElement())
    stash.push(generateElement())

return stash returns the array :+1:

What do the rest of the lines of code do?

1 Like

When the tests run an error is being generated but it’s hidden.

InternalError: too much recursion

haha you helped me much, i confused between recursive and functional programming, you right . i no need to those lines

  if(num < 1) {
    return
  }
 generateArray(num - 1)

now i deleted 'em and i passed both of tests i was fail in :slight_smile:

so now you helped me solve this part of tests, if i get stuck again will i need to create new topic or this topic will be still open ?

um how can i discover those hidden errors, in chrome dev tools console ?!

1 Like

The error does appear there, but so do a lot of other messages. I’m not sure that would be useful.

Your function did appear to technically satisfy the tests which needed a bit more investigation. If that happens again it’s best to just create a thread here.

Yeah, may as well create a new thread if you have problems for this lab again, other wise this thread will get very long.

Could put the failing test in the title to differentiate them.

1 Like