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

218
Visualizações
Random text in javaScript

I'am trying to make a random function that every time that apper, apper with a different text

I have done a array like this:

let text = ['Oh Noo!!', 'You lost!', 'Try a next time!!'];

and here is the function:

function drawGameEnd() {
  
  if (gameOver || gameWin) {
      text = "Congrats!🥳";
    if (gameOver) {
      text = text[Math.floor(Math.random() * text.length)];
    }

    ctx.fillStyle = "#2215d6";
    ctx.fillRect(0, canvas.height / 4, canvas.width, 190); //canvas text gradientes

    ctx.font = "28px Comic Neue";
    const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
    gradient.addColorStop("0", "#fff");

    ctx.fillStyle = gradient;
    ctx.fillText(text, 20, canvas.height / 2.2);

  }
}

But when a run it, it only show one letter of the word... How can i fix it?

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

0

That's because you defined text as a string

const texts = ['Oh Noo!!', 'You lost!', 'Try a next time!!'];;
text = texts[Math.floor(Math.random() * texts.length)];
console.log(text)

about 4 years ago · Juan Pablo Isaza Relatório

0

It's because you are reassigning the variable text which holds your array of text options, to hold the single word you chose. The next time drawGameEnd is called, rather than selecting a single word in the original array, it selects a single letter from that word.

Only reason this bug didn't result in an error is because a string is kinda like an array of characters, so syntactically it checks out for the first two times drawGameEnd is called... untyped JS is wild.

about 4 years ago · Juan Pablo Isaza Relatório

0

You've defined a variable called text and set it to an array of strings:

let text = ['Oh Noo!!', 'You lost!', 'Try a next time!!'];

Then what do you do with text?...

text = "Congrats!🥳";

or:

text = text[Math.floor(Math.random() * text.length)];

Now it's not an array. Now it's a string. The array is gone. (And, later, if you treat a string as an array then it's an array of characters.)

The lesson here is that you shouldn't try to store everything in the same variable. Because once you do, you overwrite whatever was previously stored in that variable.

Use different variable names. And convey information about what the variable is in those names. For example:

let textOptions = ['Oh Noo!!', 'You lost!', 'Try a next time!!'];
let text = '';

and:

if (gameOver || gameWin) {
  text = "Congrats!🥳";
  if (gameOver) {
    text = textOptions[Math.floor(Math.random() * textOptions.length)];
  }
  //...
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