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

136
Views
Ejercicio de Javascript "hacer que todos los valores sean ajustables por el usuario"

Tengo este desafío de codificación de Javascript para principiantes en el que tengo que generar caracteres aleatorios y el usuario elige el ancho, la altura y el "aleatorio" es la cantidad de veces que el carácter aleatorio aparece en el ciclo, según entendí.

hacer que todos los valores sean ajustables por el usuario

ingrese la descripción de la imagen aquí

Sé que esta solución es muy simple, pero parece que no puedo alcanzarla. Este es mi código hasta ahora:

 <script type="text/javascript"> let width = prompt("Choose a Width value"); let height = prompt ("Choose a height value"); let random = prompt ("Choose a random value"); let string = ""; for(let i = 0; i < height; i++) { for(j = 0; j < width; j++) { string += "#"; } string += "<br>"; } const characters = '*'; function generateRandomCode(){ let result = "" let charactersLength = characters.length; for (let i = 0; i < random ; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result } document.write(string); </script>

¿Alguien sabe cómo generar estos caracteres aleatorios y hacer que el usuario elija la cantidad de veces que lo hace? Gracias <3

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

0

Las cadenas son inmutables. Podría ser más fácil con una matriz 2d.

 document.getElementById("calc").onclick = () => { const width = document.getElementById("width").value; const height = document.getElementById("height").value; const random = document.getElementById("random").value; // Create the 2d array, filled with '#' var arr = Array.from({length: height}, () => Array.from({length: width}, () => '#')); // Replace in array at random indexes let replaced = 0; while (replaced < random) { const x = Math.floor(Math.random() * height); const y = Math.floor(Math.random() * width); if (arr[x][y] !== '*') { arr[x][y] = '*'; replaced += 1; } } // Convert to string let result = arr.join('<br/>').replaceAll(',',''); // Display result document.getElementById("result").innerHTML = `<pre>${result}</pre>`; };
 <div> <label for="width">Width:</label> <input type="number" id="width" value="5" /> </div> <div> <label for="height">Height:</label> <input type="number" id="height" value="5" /> </div> <div> <label for="random">Random:</label> <input type="number" id="random" value="5" /> </div> <div> <button id="calc">Go</button> </div> <div id="result">

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!