-2

function stringGen(num) {
  var result = "";
  var alpha =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  var alpha1 = alpha.length;
  for (var i = 0; i < num; i++) {
    result += alpha.charAt(Math.floor(Math.random() * alpha1));
  }
  return result;
}

var number = document.getElementById("num");
console.log(stringGen(number));
<html>
  <body>
    <div>
      Enter the length of character:
      <input type="text" id="num" />
      <button onclick="stringGen(5)">submit</button>
      <p id="result"></p>
    </div>
    <script type="text/javascript" src="app/index.js"></script>
  </body>
</html>

Please find the error. I'm not getting the answer to this. I want to display the random text with the length from the value given in the text field in HTML.

Rags
  • 15
  • 2

1 Answers1

0

I have update your code to fix not showing random text . At first i get input data by var num = +document.getElementById("num").value; I use + for convert into integer. you code is not showing becouse you do not write any code to show. use document.getElementById("result").innerHTML =result to show in div.

function stringGen(num) {
  var result = "";
  var alpha =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  var alpha1 = alpha.length;
  for (var i = 0; i < num; i++) {
    result += alpha.charAt(Math.floor(Math.random() * alpha1));
  }
  return result;

}

function showResult(){
  var num = +document.getElementById("num").value;
  document.getElementById("result").innerHTML =stringGen(num);
 }
<div>
      Enter the length of character:
      <input type="text" id="num" />
      <button onclick="showResult()">submit</button>
      <p id="result"></p>
    </div>
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33