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

141
Views
Reduzca el número de líneas en un interruptor/caso con JavaScript

Creé un piano y tengo un case para cada tecla. Hay 61 de ellos.

Tengo una caja de interruptores para las teclas y para las teclas. ¡Entonces 122 casos!

Te muestro un ejemplo para 3 teclas:

 switch (e.key) { // first row white keys case '&': document.getElementById("1").classList.add("active") if (audio1.paused) { audio1.play() } else { audio1.play() audio1.currentTime = 0 } break; case 'é': document.getElementById("2").classList.add("active") if (audio2.paused) { audio2.play() } else { audio2.play() audio2.currentTime = 0 } break; case '"': document.getElementById("3").classList.add("active") if (audio3.paused) { audio3.play() } else { audio3.play() audio3.currentTime = 0 } break;

Aquí está el enlace si quieres: ver el piano

¿Alguna idea de cómo puedo optimizar esto?

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

0

Puede crear un mapa hash de las claves para las identificaciones de los jugadores, algo como esto:

 const elems = { '&': '1', 'é': '2', '"': '3', }; function toggleAudio(e) { const id = elems[e.key]; const player = document.getElementById(id).classList.add('active'); if (!player.paused) player.currentTime = 0 player.play() }
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!