Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
¿Cómo se pasa un clic de botón como argumento de función?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Coin Flip</title> </head> <body> <h1>Coin Flip</h1> <h3>Let's flip a coin! Choose heads or tails:</h3> <button onclick="flip()" id="choice">Heads</button> <button onclick="flip()" id="choice">Tails</button> <h3 id="guess"></h3> <h3 id="result"></h3> <h3 id="confirm"></h3> </body> <script src="scripts.js"></script> </html>
 function flip(choice) { // Declares random number variable var randomNumber=Math.floor(Math.random()*2)+1; // Conditional random win/loss if(randomNumber==1){ document.getElementById("guess").innerHTML = "You guessed " + choice + "..."; document.getElementById("result").innerHTML = "The coin flips and comes up Tails!"; document.getElementById("confirm").innerHTML = "Good Guess!"; } else { document.getElementById("guess").innerHTML = "You guessed " + choice + "..."; document.getElementById("result").innerHTML = "The coin flips and comes up Heads!"; document.getElementById("confirm").innerHTML = "Good Guess!"; } }

Estoy haciendo un simple juego de tirar monedas usando HTML y JS. Tengo problemas para imprimir la elección que hace el usuario (cara o cruz). ¿Hay alguna manera de pasar el botón en el que hicieron clic como el texto "cabezas" o como el texto "colas"? Verifique en JS el texto que se imprimirá.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simplemente pase cara o cruz como parámetro en la llamada a la función:

 <button onclick="flip('Heads')" id="choice">Heads</button> <button onclick="flip('Tails')" id="choice">Tails</button> 

 function flip(choice) { // Declares random number variable var randomNumber=Math.floor(Math.random()*2)+1; // Conditional random win/loss if(randomNumber==1){ document.getElementById("guess").innerHTML = "You guessed " + choice + "..."; document.getElementById("result").innerHTML = "The coin flips and comes up Tails!"; document.getElementById("confirm").innerHTML = "Good Guess!"; } else { document.getElementById("guess").innerHTML = "You guessed " + choice + "..."; document.getElementById("result").innerHTML = "The coin flips and comes up Heads!"; document.getElementById("confirm").innerHTML = "Good Guess!"; } }
 <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Coin Flip</title> </head> <body> <h1>Coin Flip</h1> <h3>Let's flip a coin! Choose heads or tails:</h3> <button onclick="flip('Heads')" id="choice">Heads</button> <button onclick="flip('Tails')" id="choice">Tails</button> <h3 id="guess"></h3> <h3 id="result"></h3> <h3 id="confirm"></h3> </body> <script src="scripts.js"></script> </html>

about 4 years ago · Juan Pablo Isaza Report

0

No duplique las identificaciones. Para más detalles ver comentarios en el ejemplo.

 // Bind click event to each <button> document.querySelectorAll('button').forEach(btn => btn.onclick = flip); // Define event handler -- pass Event Object function flip(e) { // Reference the <button> user clicked const picked = e.target; // Get the value of picked and convert string to number with '+' let side = +picked.value; // Get random integer in the range of 1 to 2 let outcome = Math.floor(Math.random()*2)+1; // Reference <output> const out = document.querySelector('output'); // If the value of picked equals the random number... if (side === outcome) { // Assign winning string with picked id interpolated out.value = `${picked.id}! You win!`; } else { // Otherwise assign losing string to <output> out.value = `${picked.id}! You lose!`; } }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Coin Flip</title> </head> <body> <h1>Coin Flip</h1> <p>Let's flip a coin! Choose heads or tails:</p> <button id='Heads' value='1'>Heads</button> <button id='Tails' value='2'>Tails</button> <p><output></output></p> </body> <script src="scripts.js"></script> </html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!