Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda