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

91
Visualizações
Unable to modify a variable inside a function in JS

I'm using React, so I don't know if the rules are different for it, but I'm making a voice assistant, I want the variable myVar to be modified in the switch statement in the function myFunc. Here is what I tried now:

  let myVar = "x";
  const myFunc = () => {
    myVar = "Welcome Back!";
    switch (transcript) {
      case "hello":
      case "hey":
      case "hi":
        const greetingArray = ["Hello", "Hello! Happy day!"];
        const randomItem =
          greetingArray[Math.floor(Math.random() * greetingArray.length)];
        myVar = randomItem;
        TTS(myVar, "english");

        break;
      case "bye":
        TTS("Goodbye", "english");
        break;
    }

    return myVar;
  };

  return (
    <div>
      <p>Microphone: {listening ? "on" : "off"}</p>
      <button onClick={SpeechRecognition.startListening}>Start</button>
      <button onClick={SpeechRecognition.stopListening}>Stop</button>
      <button onClick={resetTranscript}>Reset</button>
      <button
        onClick={() => {
          console.log({ transcript });
        }}
      >
        Click
      </button>

      <p>{transcript}</p>
      <p>{myVar}</p>
      <button onClick={console.log(myVar)}>Button</button>
      <button onClick={myFunc}>Speak</button>
    </div>
  );

But the problem is that it is not working. It is modified inside the function, but it is not coming outside it.

Can anyone please help me?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

if you're using react.js, maybe you should define your variable in the state of the component

for example

const myComponent = () => {
  const [myVar, setMyVar] = useState("");

  const yourFunction = () => {
    ... 

    setMyVar(new_value)
  }

  return <div>{ myVar }</div>;
}

Just use setMyVar to update your new value.

hope this might help you.

about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe try defining the variable in the function?

   const myFunc = () => {
    let myVar = "x";
    myVar = "Welcome Back!";
    switch (transcript) {
      case "hello":
      case "hey":
      case "hi":
        const greetingArray = ["Hello", "Hello! Happy day!"];
        const randomItem =
          greetingArray[Math.floor(Math.random() * greetingArray.length)];
        myVar = randomItem;
        TTS(myVar, "english");

        break;
      case "bye":
        TTS("Goodbye", "english");
        break;
    }

    return myVar;
  };
about 4 years ago · Juan Pablo Isaza Relatório
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