Trying to populate 16 random images using Math.random and a filter


Trying to populate 16 random images using Math.random and a filter



``import React from 'react'
import { getCard } from '../redux'
import { connect } from 'react-redux'
import CardBoard from './CardBoard'


class Board extends React.Component{
componentDidMount(){
this.props.getCard()
}
render(){
let gameCard = Math.floor(Math.random() * (54 - 2) +2);
const game = () => {
let cards = ;
if(cards.length < 16) {
cards.push(gameCard);
}
return cards;
};
return(


{this.props.cards
.filter(one => one.number === gameCard)
.map(card => (

))}

);
}
}

export default connect(state => state, { getCard })(Board)``





Welcome to stackoverflow B L! There is not quite enough information in your question for us to help you, sadly. Do you think you could include your entire component?
– Tholle
11 mins ago





Thank you Tholle, I have updated to include the entire component
– B L
39 secs ago




1 Answer
1



In your if statement you always push just 1 item into your array. Here:


let cards = ;
if (cards.length < 16) {
cards.push(gameCard);
}



Instead of this, try a do while statement:


do {
cards.push(gameCard);
}
while (cards.length <= 16);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

The forked VM terminated without saying properly goodbye. VM crash or System.exit called