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

104
Views
¿Cómo asignar un valor de temperatura aleatorio para cada fecha de una matriz basada en un mes de 30 elementos de fecha/día?

Imagina un mes de 30 días y asigna al azar una temperatura para cada uno de estos días, incluyendo la temperatura de cada día entre 7 y 16°C. Aquí está mi código:

 let arrMonth = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ] let temperature = Math.floor(Math.random() * (16 - 7 + 1)) + 7; for (let i = 0; i < arrMont.length; ++i) { arrMonth[i] = temperature console.log(arrMonth[i]) }

Solo me da la temperatura del ultimo dia. ¿Que estoy haciendo mal?

en la consola:

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

0

Una extensión simple de su código es convertir su variable en una función de flecha, como

 const temperature = () => (Math.floor(Math.random() * (16 - 7 + 1)) + 7);

Luego, cada vez que solicite 'temperatura ()' obtendrá un nuevo valor.

 let arrMonth = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ] const temperature = () => (Math.floor(Math.random() * (16 - 7 + 1)) + 7); for (let i = 0; i < arrMonth.length; ++i) { arrMonth[i] = temperature() console.log(arrMonth[i]) }

about 4 years ago · Juan Pablo Isaza Report

0

Lo que pide el OP es en realidad una tarea de ping de map .

Pero dado que el OP también quiere representar las fechas/días bien templados, se debe tener en cuenta un enfoque que siga la separación de preocupaciones (dicho libremente ... "cada tarea una sola función" ).

Por lo tanto, el OP podría jugar con las diferentes tareas y, por ejemplo, adaptarlas a las necesidades del OP ...

 function getRandomValueFromTemperatureRange() { // returns random values in between 7 and 16 inclusively. return (Math.floor(Math.random() * 10) + 7); } function createTemperedDateItem(date) { return { date, temperature: getRandomValueFromTemperatureRange(), }; } function renderTemperedDateItems(dateList) { const listRootNode = document.createElement('dl'); dateList.forEach(dateItem => { const { date, temperature } = dateItem; const dateNode = document.createElement('dt'); const temperatureNode = document.createElement('dd'); dateNode.title = 'Date within Month'; temperatureNode.title = 'Temperature in °C'; dateNode.textContent = date; temperatureNode.textContent = temperature; listRootNode.appendChild(dateNode); listRootNode.appendChild(temperatureNode); }); document.body.appendChild(listRootNode); } const arrMonth = Array.from( { length: 30}, (elm, idx) => (idx + 1) ); console.log({ arrMonth, temperedDateList: arrMonth.map(createTemperedDateItem), }); renderTemperedDateItems( arrMonth.map(createTemperedDateItem) );
 dl { margin: 0!important; } dl::after { clear: both; display: block; content: ''; } dt { float: left; } dd::after { display: inline; content: ' °C'; } body { margin: 0 0 100px 0!important; } .as-console-wrapper { max-height: 100px!important; }

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!