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

132
Views
La declaración de cambio de Javascript no funciona

Este código funciona perfectamente. Pero si elimino el comentario de la declaración de cambio, falla por completo. La declaración de cambio parece ser correcta. Cualquier ayuda será apreciada.

 function draw(n) { let canvas = document.querySelector('#my-canvas'); let context = canvas.getContext('2d'); context.fillStyle = 'black' let contourInterval = 1500 let contourWidth = 500 let fsize = 1000 let x0 = fsize / 2 let y0 = fsize / 2 for (j = 0, y = -y0; j < fsize; j++, y++) { for (i = 0, x = -x0; i < fsize; i++, x++) { /* switch(n) { case 0: let fun = (x*x + y*y) break; case 1: let fun = (x*x - y*y) break; case 2: let fun = 1.0/(x*x + y*y) break; default: let fun = (x*x + y*y) } */ let fun = (x * x + y * y) if (Math.abs(fun % contourInterval) < contourWidth) { context.fillRect(x + x0 / 2, y + y0 / 2, 1, 1) } } } } draw(1)
 <canvas id="my-canvas" width="500" height="500"></canvas>

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

El problema es que está declarando la variable fun dentro de la declaración de switch y luego está tratando de usarla afuera más tarde.

Intenta actualizar tu código a lo siguiente:

 function draw(n) { let canvas = document.querySelector('#my-canvas'); let context = canvas.getContext('2d'); context.fillStyle = 'black' let contourInterval = 1500 let contourWidth = 500 let fsize = 1000 let x0 = fsize / 2 let y0 = fsize / 2 for (j = 0, y = -y0; j < fsize; j++, y++) { for (i = 0, x = -x0; i < fsize; i++, x++) { let fun = 0 // declare this here outside the scope of the switch statement switch(n) { case 0: // remember to get rid of the `let` keywords in your `case` statments fun = (x*x + y*y) break; case 1: fun = (x*x - y*y) break; case 2: fun = 1.0/(x*x + y*y) break; default: fun = (x*x + y*y) } // now `fun` can be used here without scope problems if (Math.abs(fun % contourInterval) < contourWidth) { context.fillRect(x + x0 / 2, y + y0 / 2, 1, 1) } } } } draw(1)
 <canvas id="my-canvas" width="500" height="500"></canvas>

De esta manera, declara la variable en el ámbito externo y usa la declaración de cambio para establecerla. Ahora se puede utilizar en el ámbito exterior.

about 4 years ago · Santiago Gelvez Report

0

Declara diversión primero

 function draw(n) { let canvas = document.querySelector('#my-canvas'); let context = canvas.getContext('2d'); context.fillStyle = 'black' let contourInterval = 1500 let contourWidth = 500 let fsize = 1000 let x0 = fsize / 2 let y0 = fsize / 2 for (j = 0, y = -y0; j < fsize; j++, y++) { for (i = 0, x = -x0; i < fsize; i++, x++) { let fun = 0 switch(n) { case 0: fun = (x*x + y*y) break; case 1: fun = (x*x - y*y) break; case 2: fun = 1.0/(x*x + y*y) break; default: fun = (x*x + y*y) } if (Math.abs(fun % contourInterval) < contourWidth) { context.fillRect(x + x0 / 2, y + y0 / 2, 1, 1) } } } } draw(1)
 <canvas id="my-canvas" width="500" height="500"></canvas>

about 4 years ago · Santiago Gelvez 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!