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

159
Visualizações
How do I choose a random textContent?

Edit: my Js fiddle link didn't attach, trying again: https://jsfiddle.net/jbLd3kt1/

I am trying to make a simple generator with Javascript just for fun. It's an outfit generator with different combinations of clothing.

Right now I have two buttons for two types of outfits (I would like to add more in the future). I want to instead have one button, and it will choose an outfit type (#falloutfita or #falloutfitb) and then generate the random outfit based on that.

I thought maybe I could use an "or" || with the textContent but it looks like it's not possible.

Here is my Javascript code up through the first outfit type:

var allsweaters = ["blue hoodie", "black hoodie", "pink v-neck sweater", "gray pullover"];
var allpants = ["black casual pants", "dark old navy jeans", "medium old navy jeans", "black jumpsuit"];
var alllongshirts = ["long-sleeved white shirt", "long-sleeved gray shirt", "long-sleeved black shirt"];
var allovershirts = ["white button-up", "green jacket", "blue jean shirt"];

document.querySelector("#submitfalla").addEventListener("click", () => {
  var sweater = "";
  var pants = "";
  var lsshirt = "";
  var random_index = 0;
  for (let i = 0; i < 1; i++) {
    random_index = Math.floor(Math.random() * allsweaters.length);
    sweater += allsweaters[random_index];
    random_index = Math.floor(Math.random() * allpants.length);
    pants += allpants[random_index];
    random_index = Math.floor(Math.random() * allpants.length);
    lsshirt += alllongshirts[random_index];
  }

  document.querySelector("#falloutfita").textContent = sweater + " and " + lsshirt + " and " + pants;
});
<button id="submitfalla">Submit</button>

<div id="falloutfita"></div>

It's my first post on stack overflow, please let me know if there is any way I should edit this question too! I've looked at similar questions here but haven't been able to successfully apply them to mine.

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

What do you think about that?

const allSweaters   = ['blue hoodie', 'black hoodie', 'pink v-neck sweater', 'gray pullover']
const allPants      = ['black casual pants', 'dark old navy jeans', 'medium old navy jeans', 'black jumpsuit']
const allLongShirts = ['long-sleeved white shirt', 'long-sleeved gray shirt', 'long-sleeved black shirt']
const allOverShirts = ['white button-up', 'green jacket', 'blue jean shirt']

function getRandomlyFrom (array) {
  return array[Math.round(Math.random() * (array.length - 1))]
}

function getRandomOutfit () {
  return [
    getRandomlyFrom(allSweaters),
    getRandomlyFrom(allPants),
    getRandomlyFrom(
      getRandomlyFrom([allLongShirts, allOverShirts]) // return one of the two arrays
    )
  ].join(' and ')
}

function onClick () {
  document.querySelector('#falloutfita').textContent = getRandomOutfit()
}

document.querySelector('#submitfalla').addEventListener(onClick)

almost 4 years ago · Santiago Trujillo 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