Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
trying to resolve no-unused-expression error within for loop leads to react component function not working

I'm trying to build a react component that allows a die to be rolled and update the total of the die roll on the page.I'm only including what I perceive to be necessary as the function is pretty long but this should cover the problems I'm having

import "./Dice.css";
import img from "./img/D8.png";

class Dice extends Component {
  constructor(props) {
    super(props);
    this.state = {
      bonusRoll: 0,
      penaltyRoll: 0,
      totalRoll: 1
    }; 
    this.rollDie = this.rollDie.bind(this);
 };
 rollDie(bonus,penalty) {
    let rolls = []
    const reducer = (previousValue, currentValue) => previousValue + currentValue;
    console.log("test1")
    if (!bonus && !penalty){
      console.log("test2")
      for (let i = 0; i < 2; i++) { 
       rolls.push(Math.ceil(Math.random() * 8)) * i; // within my code this is line 41
       }; 
      this.setState({totalRoll: rolls.reduce(reducer)});
      // return rolls.reduce(reducer) // this worked when the function was called in vanilla JS
      }};

render() {
    return (
      <div className="Dice">
        <div className="Dice-rolls-wrap">
          <div className="Dice-base-roll">
          <h5>Baseroll:&nbsp;{this.state.totalRoll}</h5>
          </div>
        </div>
          <button className="Dice-btn" onClick={this.rollDie}>
          <img className="Dice-img" src={img} alt="Dice icon" />
        </button>
      </div>
    );
  }
}

export default Dice;

with the above code I get the following error when trying to run/build the app

src/Dice.js
  Line 41:9:  Expected an assignment or function call and instead saw an expression  no-unused-expressions

Search for the keywords to learn more about each error.

if I add a return statement on line 41 everything renders but when clicking the button it will not proceed past the conditional (EG: "test1" shows in console but "test2" doesn't) I'm not passing in arguments to the function so it should be responding to this first bit of logic as far as I can tell but I'm not sure where I'm going wrong. (I'm also probably using setState wrong but I can't get the code to reach there yet to say). Also please be gentle this is my first Stack Overflow question

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda