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

116
Views
Cambiar el tamaño de las columnas para que se ajusten a la longitud - JavaScript - Matemáticas

Dado el ancho de una cuadrícula y el tamaño objetivo de una columna, quiero calcular un nuevo tamaño para la columna, que sea lo más cercano posible al tamaño objetivo.

Por ejemplo:

 gridSize: 10, targetSize: 9, newSize: 10 gridSize: 100, targetSize: 51, newSize: 50 gridSize: 20, targetSize: 6, newSize: 6.66667 ...

Visualización:

ingrese la descripción de la imagen aquí

Si el tamaño objetivo es más grande que la cuadrícula, debería ser el ancho de la cuadrícula predeterminado.

Aquí hay algo de código:

 function calculateSize(gridWidth, targetSize){ var newSize = 0; if(targetSize > gridWidth){ // The target size is larger than the grid, default to maximum width newSize = gridWidth; } else{ // Calculate new size, that is as close to the target size as possible var count = gridWidth / targetSize; var difference = Math.round(gridWidth / count) - gridWidth / count; // I don't know how to reach the desired result with these two values, maybe my approach is wrong } return newSize; }

Estoy seguro de que es muy fácil, pero las matemáticas no son mi fuerte. ¿Alguien sabe cómo calcular esto? ¡Gracias!

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

0

posibilidades

 function calculateSize(gridWidth, targetSize) { let inOut = { in:{}, out:{} } inOut.in.count = Math.floor( gridWidth / targetSize ) inOut.in.size = Math.floor(gridWidth / inOut.in.count) inOut.in.sizefloat = gridWidth / inOut.in.count inOut.in.delta = gridWidth - (inOut.in.count * inOut.in.size ) inOut.out.size = inOut.in.size +1 inOut.out.count = inOut.in.count inOut.out.delta = (inOut.out.count * inOut.out.size ) - gridWidth inOut.best = (inOut.out.delta < inOut.in.delta) ? `${inOut.out.count} columns of ${inOut.out.size} pixels (out)` : `${inOut.in.count} columns of ${inOut.in.size} pixels (in)` return inOut } console.log( calculateSize(20,6) )
 .as-console-wrapper {max-height: 100%!important;top:0 }

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!