|  | 
|  | 1 | +/* | 
|  | 2 | +Random Character id | 
|  | 3 | + | 
|  | 4 | +Create a Javascript function, which generates a random character id comprising of alphabets and numbers for given input length. | 
|  | 5 | +Hint: Return the generated random character id in the function stringGen(). | 
|  | 6 | + | 
|  | 7 | +Given HTML Code: | 
|  | 8 | +<html>  | 
|  | 9 | +<body>  | 
|  | 10 | +<div>  | 
|  | 11 | +Enter the length of character:  | 
|  | 12 | +<input type='text' id="num">  | 
|  | 13 | +<button onclick="stringGen()">submit</button>  | 
|  | 14 | +<p id="result"></p> | 
|  | 15 | +</div>  | 
|  | 16 | +<script type="text/javascript" src="index.js"></SCRIPT>  | 
|  | 17 | +</body>  | 
|  | 18 | +</html> | 
|  | 19 | + | 
|  | 20 | +Given JS Code:  | 
|  | 21 | +function stringGen() | 
|  | 22 | +{ | 
|  | 23 | + //Type your code here. | 
|  | 24 | + return //Enter your return statement  | 
|  | 25 | +} | 
|  | 26 | +*/ | 
|  | 27 | +------------------------------------------------------------------------------------------------------------------------------------- | 
|  | 28 | + | 
|  | 29 | +<!DOCTYPE html> | 
|  | 30 | +<html> | 
|  | 31 | +<body> | 
|  | 32 | + | 
|  | 33 | +<div>  | 
|  | 34 | +Enter the length of character:  | 
|  | 35 | +<input type='text' id="num">  | 
|  | 36 | +<button onclick="stringGen()">submit</button>  | 
|  | 37 | +<p id="result"></p> | 
|  | 38 | +</div>  | 
|  | 39 | + | 
|  | 40 | +<script> | 
|  | 41 | + | 
|  | 42 | +function stringGen() | 
|  | 43 | +{ | 
|  | 44 | + //Type your code here. | 
|  | 45 | + var len= document.getElementById("num").value;  | 
|  | 46 | + var text = ""; | 
|  | 47 | + var alpCount=0; | 
|  | 48 | + var digCount=0; | 
|  | 49 | + var charList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | 
|  | 50 | +for(var i=0; i < len; i++ ) | 
|  | 51 | +{  | 
|  | 52 | +text += charList.charAt(Math.floor(Math.random() * charList.length)); | 
|  | 53 | + | 
|  | 54 | +//Count the presence of digits and alphabets. | 
|  | 55 | +if(text.charAt(i)>=0&&text.charAt(i)<=9){ | 
|  | 56 | + digCount++; | 
|  | 57 | + } | 
|  | 58 | +else{ | 
|  | 59 | + alpCount++; | 
|  | 60 | + } | 
|  | 61 | +} | 
|  | 62 | + | 
|  | 63 | +//If the generated charcterId doesn't contain any number or any aplhabet, insert one because it should contain both. | 
|  | 64 | +if(digCount==0){ | 
|  | 65 | +var randNum= Math.floor(Math.random()*10); | 
|  | 66 | +//Insert any random number at the last position(or position of your choice). | 
|  | 67 | +var finText= text.replace(text.charAt(text.length-1),randNum); | 
|  | 68 | +document.getElementById("result").innerHTML = finText; | 
|  | 69 | +return finText; | 
|  | 70 | +} | 
|  | 71 | +else if(alpCount==0){ | 
|  | 72 | +var randNum= Math.floor(Math.random() * (90 - 65 + 1) ) + 65; | 
|  | 73 | +//Insert any random alphabet(small or capital letter as per your choice) at the last position(or position of your choice). | 
|  | 74 | +var finText= text.replace(text.charAt(text.length-1),String.fromCharCode(randNum)); | 
|  | 75 | +document.getElementById("result").innerHTML = finText; | 
|  | 76 | +return finText; | 
|  | 77 | +} | 
|  | 78 | +else{ | 
|  | 79 | +document.getElementById("result").innerHTML = text; | 
|  | 80 | +return text; | 
|  | 81 | +} | 
|  | 82 | +} | 
|  | 83 | +</script> | 
|  | 84 | +</body> | 
|  | 85 | +</html>  | 
|  | 86 | + | 
0 commit comments