Generating id’s and pushing them into an array in Javascript? [closed]

I have a function generating color id’s:

const hexaColor = () => {
  let str = '0123456789abcdef'
  let color = ''
  for (let i = 0; i < 6; i++) {
    let index = Math.floor(Math.random() * str.length)
    color += str[index]
  }
  return '#' + color
}

And then I’m using another function, as I want to have an array of six generated colors:

const colorArray= ()=> {  
    let colors =  []
    for (let i=0; i>=6; i++ ){
        colors.push(hexaColor[i])
        console.log(colors)
    }
    return colors;   
    
    }
console.log(colorArray())

however, what I see in a console is just an empty array. What am I doing wrong? Any help appreciated.

Answer

You have a code error

The loop did not run at all

The condition will never be meti>=6

Change the condition at the top of the loop

    for (let i=0; i>=6; i++ ){

This is how it works

    for (let i=0; i<=6; i++ ){

also this line fix to

colors.push(hexaColor()[i])

Leave a Reply

Your email address will not be published. Required fields are marked *