控制台说Math.Floor不是功能。我该如何解决

时间:2019-08-14 14:58:30

标签: javascript reactjs jsx

我正在做石头,纸,剪刀游戏和数学运算。由于它在我的条件渲染语句中说它是一个功能,所以地板不起作用。

我尝试消除math.floow,只将其放在随机数起源的地方,但这并不起作用,因为它说的是同一件事。

constructor() {
    super();
    this.state = {
        rockPaperScissorsComputer: ['rock', 'paper', 'scissors'],
        rockPaperScissorsUser: null,
        random: null
    };
    this.handleClickRock = this.handleClickRock.bind(this);
    this.handleClickPaper = this.handleClickPaper.bind(this);
    this.handleClickScissors = this.handleClickScissors.bind(this);
}
handleClickRock() {
    const min = 0;
    const max = 3;
    const random = min + (Math.random() * (max - min));
    this.setState({ random })
    this.setState({
        rockPaperScissorsUser: 'rock'
    })
}
handleClickPaper() {
    const min = 0;
    const max = 3;
    const random = min + (Math.random() * (max - min));
    this.setState({ random })
    this.setState({
        rockPaperScissorsUser: 'paper'
    })
}
handleClickScissors() {
    const min = 0;
    const max = 3;
    const random = min + (Math.random() * (max - min));
    this.setState({ random })
    this.setState({
        rockPaperScissorsUser: 'scissors'
    })
}

render() {
    return (
        <div>
            <button value="Click me!" onClick={this.handleClickRock}>Rock</button>
            <button value="Click me!" onClick={this.handleClickPaper}>Paper</button>
            <button value="Click me!" onClick={this.handleClickScissors}>Scissors</button>
            <h1> {this.state.rockPaperScissorsComputer[Math.floor(this.state.random)]} </h1>
            {   
                this.state.rockPaperScissorsComputer[Math.floor(this.state.random)] == 
                this.state.rockPaperScissorsUser ? <h1> It was a tie </h1> :
                this.state.rockPaperScissorsUser == 
                'rock' && Math.Floor(this.state.random) == 2
                || this.state.rockPaperScissorsUser == 'paper' && 
                Math.Floor(this.state.random) == 0 || 
                this.state.rockPaperScissorsUser =='scissors' && 
                Math.Floor(this.state.random) == 1
                ? <h1>You win</h1> : <h1>You lost!</h1> 

            } 


        </div>
    );


}

我希望代码能够输出功能齐全的剪刀石头布游戏,但是,当我单击按钮时,它给我一个错误:“ Math.Floor is not function”。

2 个答案:

答案 0 :(得分:0)

使用Math.floor,floor必须小写。

答案 1 :(得分:0)

JavaScript是区分大小写的语言。您需要为floor

使用小写字母
Math.floor()

不是

Math.Floor()