Need HTML and JS code.
Write a Javascript function named RandomString(randomStringLength) that returns a string filled with random characters where the length of the random string is defined by the input parameter.
HINT: Use a for-loop, Math.random, and the fact that the alphabet has 26 characters (remembering that we can have both lower-case and upper-case characters in a string).
This is what I have:
<html>
<!-- inputbox.html Jerry Davis -->
<!-- Web page that sets the correct letter value. -->
<!-- ========================================== -->
<head>
<script>function RandomString(randomStringLength){
var text = "";
var char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i=0; i < randomStringLength ; i++ )
text += char_list.charAt(Math.floor(Math.random() * char_list.length));
return text;
}
</script>
</head>
<body>
<input id="random"/>
<button onclick="myFunction()">random output</button>
<p id="demo"></p>
</body>
</html
------
What am I doing wrong
I have corrected your code. Bold part are the changes. You didnt implemented the myFunction method which is called on click of button: -
<html>
<!-- inputbox.html Jerry Davis -->
<!-- Web page that sets the correct letter value. -->
<!-- ========================================== -->
<head>
<script>
function RandomString(randomStringLength){
var text = "";
var char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i=0; i < randomStringLength ; i++ )
text += char_list.charAt(Math.floor(Math.random() * char_list.length));
return text;
}
function myFunction() {
var num =
document.getElementById("random").value;
var output = "";
if(num == null || num == '') {
output = "Please enter a length in
the input box and Click on random output button again.";
} else {
output = RandomString(num);
}
document.getElementById("demo").innerHTML=output;
}
</script>
</head>
<body>
<input id="random"/>
<button onclick="myFunction()">random output</button>
<p id="demo"></p>
</body>
</html>
Output Samples: -
Get Answers For Free
Most questions answered within 1 hours.