top of page

Code to find the factorial of a number

<!DOCTYPE html>

<html>

<head>

</head>

<body style = "text-align: center; font-size: 20px;">

<h1> Welcome to the javaScript world!! </h1>

Enter a particular number: <input id = "num">

<br><br>

<button onclick = "fact()"> Please type any Factorial number </button>

<p id = "res"></p>

<script>

function fact(){

var i, num, f;

f = 1;

num = document.getElementById("num").value;

for(i = 1; i <= num; i++)  

{

f = f * i;

}

i = i - 1;  

document.getElementById("res").innerHTML = "The factorial of the number " + i + " is: " + f ;

}

</script>

</body>

</html>

Code to run a javaScript timer

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Timing Sample</h2>

<p>Click on "Try it". Wait 5 seconds, and the page will alert "Hello How are you!!".</p>

<button onclick="setTimeout(myFunction, 5000);">Try it</button>

<script>

function myFunction() {

  alert('Hello How are you!!');

}

</script>

</body>

</html>

  OUTPUT:

                                         Hello How are you!!

Code to convert number to string

<!DOCTYPE html>

<html>

<body>

<h2>HTML JS OUTPUT</h2>

<p>1st Paragraph.</p>

<p id="HTMLJSOUTPUT"></p>

<script>

document.getElementById("HTMLJSOUTPUT").innerHTML = 7 + 7;

</script>

</body>

</html>

 

 

Output:

​

                                           HTML JS OUTPUT

​

                                             1st Paragraph

​

                                                     14

Code to prompt for a message

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Prompt Example</h2>

<button onclick="myFunction()">Please Try for Prompt message</button>

<p id="Prompt Example"></p>

<script>

function myFunction() {

  let text;

  let user = prompt("Please enter your name:", "Your First Name");

  if (user == null || user == "") {

    text = "User cancelled the prompt.";

  } else {

    text = "Hello " + person + "! How are you?";

  }

  document.getElementById("Prompt Example").innerHTML = text;

}

</script>

</body>

</html>

​​

​

  OUTPUT:

 

                                                                   Please Enter Your Name:

​

​                                                                   Your First Name  

                                                                     

​

                                                                    

bottom of page