<!DOCTYPE html>
<html>
<head>
<title>Factorial calculation with a recursive function</title>
<style type="text/css">
p {margin: 0;}
</style>
<script type="text/javascript">
var output= "";
var input = parseInt(window.prompt("Please input the number"));
document.writeln("<h1> Factorials of 0 to " + input + " </h1>");
function calculateFactorials (){
for (var i=0; i <= input; i++){
output += "" + i + "! = " + factorial(i) + "<br>";
document.getElementById("hasil").innerHTML=output;
}
}
function factorial (angka){
if(angka == 0) {
return 1;
} else {
return angka * factorial(angka - 1);
}
}
window.addEventListener("load", calculateFactorials, false);
</script>
</head>
<body>
<div id="results"><p id="hasil"></div>
</body>
</html>
<html>
<head>
<title>Factorial calculation with a recursive function</title>
<style type="text/css">
p {margin: 0;}
</style>
<script type="text/javascript">
var output= "";
var input = parseInt(window.prompt("Please input the number"));
document.writeln("<h1> Factorials of 0 to " + input + " </h1>");
function calculateFactorials (){
for (var i=0; i <= input; i++){
output += "" + i + "! = " + factorial(i) + "<br>";
document.getElementById("hasil").innerHTML=output;
}
}
function factorial (angka){
if(angka == 0) {
return 1;
} else {
return angka * factorial(angka - 1);
}
}
window.addEventListener("load", calculateFactorials, false);
</script>
</head>
<body>
<div id="results"><p id="hasil"></div>
</body>
</html>
Berikut script javascriptnya, semoga dapat membantu, jika ada yang kurang paham bisa tanyakan langsung ke saya atau kotak komentar dibawah ini.
<!DOCTYPE html>
<html>
<head>
<title>Random Number Generator</title>
<style type="text/css">
ul { margin: 10px; }
li {display: inline; margin-right: 10px; }
</style>
<script type="text/javascript">
//variables used to interact with the img elements
var die1image;
var die2image;
//register button listener and get the img elements
function start(){
var button = document.getElementById("rollButton");
button.addEventListener("click", rollDice);
die1image = document.getElementById("die1");
die2image = document.getElementById("die2");
}
//roll the dice
function rollDice (){
var gambar1 = getNum();
var gambar2 = getNum();
setImage(die1image,gambar1);
setImage(die2image,gambar2);
var total = parseInt(gambar1) + parseInt(gambar2);
if (total<5){
document.getElementById("total").innerHTML = "Total : " + total + ", maaf anda kalah.";
//window.alert("Total : " + total + ", maaf anda kalah.");
}else{
document.getElementById("total").innerHTML = "Total : " + total + ", selamat anda menang.";
}
}
function getNum(){
return Math.floor(1 + Math.random() * 6 );
}
//set image source
function setImage(dieImg,nomer){
//var dieValue = Math.floor(1 + Math.random() * 6 );
dieImg.setAttribute ("src", "image/" + nomer + ".jpg");
dieImg.setAttribute ("alt", "die image with " + nomer + " spots");
}
window.addEventListener("load", start, false);
</script>
</head>
<body>
<form>
<input type="button" id="rollButton" value="Roll Dice">
</form>
<ul>
<li>
<img id="die1" src="image/1.png" alt="blank png" width="50px" height="50px">
<img id="die2" src="image/1.png" alt="blank png" width="50px" height="50px">
</li>
</ul>
<p id="total">Total : 0, tekan tombol untuk memulai</p>
</body>
</html>
<html>
<head>
<title>Random Number Generator</title>
<style type="text/css">
ul { margin: 10px; }
li {display: inline; margin-right: 10px; }
</style>
<script type="text/javascript">
//variables used to interact with the img elements
var die1image;
var die2image;
//register button listener and get the img elements
function start(){
var button = document.getElementById("rollButton");
button.addEventListener("click", rollDice);
die1image = document.getElementById("die1");
die2image = document.getElementById("die2");
}
//roll the dice
function rollDice (){
var gambar1 = getNum();
var gambar2 = getNum();
setImage(die1image,gambar1);
setImage(die2image,gambar2);
var total = parseInt(gambar1) + parseInt(gambar2);
if (total<5){
document.getElementById("total").innerHTML = "Total : " + total + ", maaf anda kalah.";
//window.alert("Total : " + total + ", maaf anda kalah.");
}else{
document.getElementById("total").innerHTML = "Total : " + total + ", selamat anda menang.";
}
}
function getNum(){
return Math.floor(1 + Math.random() * 6 );
}
//set image source
function setImage(dieImg,nomer){
//var dieValue = Math.floor(1 + Math.random() * 6 );
dieImg.setAttribute ("src", "image/" + nomer + ".jpg");
dieImg.setAttribute ("alt", "die image with " + nomer + " spots");
}
window.addEventListener("load", start, false);
</script>
</head>
<body>
<form>
<input type="button" id="rollButton" value="Roll Dice">
</form>
<ul>
<li>
<img id="die1" src="image/1.png" alt="blank png" width="50px" height="50px">
<img id="die2" src="image/1.png" alt="blank png" width="50px" height="50px">
</li>
</ul>
<p id="total">Total : 0, tekan tombol untuk memulai</p>
</body>
</html>