Sunday, March 9, 2014

Tutorial 9

Question 1. a) 

<html>
<body>

<h1>My First JavaScript</h1>
<p> Click the button to display the date.</p>

<button type="button" onclick="myFunction()"> Display Date</button>
<p id="demo"></p>

<script type="text/javascript">
     function myFunction() {
    document.getElementById("demo").innerHTML = "Thu Feb 27 2014 18:25:36 GMT +0800 Malay Peninsular Standard Time";
    }
   </script>
</body>
</html>



Question 1. b)

a)      Explain these code:
<p id="demo">
document.getElementById("demo").innerHTML = Date();4


 In order to show a text on web page using Java Script, id need to be used and the text is written double quotation to be shown on the page. But, due to the call from document.getElementById and innerHTML, the text shown is changed. Demo that was supposedly to be on the web page will be replaced by the date. The date will be shown on the page instead of the demo.

Question 1. c)


<html>
<body>

<h1>My First JavaScript</h1>
<p> Click the button to display the date.</p>

<button type="button" onclick="myFunction()"> Display Date</button>
<p id="demo"></p>

<script type="text/javascript">
function myFunction() {
var str = "Current day and time:";
  var result = str.fontcolor("red");
document.getElementById("demo").innerHTML = result + "<br><br>" + "Thu Feb 27 2014 18:39:03 GMT +0800 (Malay Peninsular Standard Time)";
    }
   </script>
</body>

</html>


Question 2

<html>
<body>

<script type="text/javascript">
var num1 = 10;
var num2 = 5;
var message1 = "Happy Birthday";
var message2 = "years old";

document.write(num1 + " + " + num2 + " = " + (num1+num2) + "<br>");
document.write(num1 + " - " + num2 + " = " + (num1-num2) + "<br>");
document.write(num1 + " * " + num2 + " = " + (num1*num2) + "<br>");
document.write(num1 + " / " + num2 + " = " + (num1/num2) + "<br><br>");
document.write(message1 + "&nbsp" + num1 + "&nbsp" + message2);
</script>

</body>
</html>

Question 3. a)

What is the output?
        Your name is: (input's name)

Question 3. b) 

<html>
<body>
<script type="text/javascript">

     var name = prompt("What is your name?", "Aaron");
     var age = prompt("What is your age?", "22");
     var answer = "Your name is&nbsp" + name + ",&nbspyou are" + "&nbsp" + age + "&nbspyears old.";
document.write(answer);
</script>
</body>
</html>

Question 3. c)

<html>
<body>

<script type="text/javascript">
var length = prompt("Enter length");
var width = prompt("Enter width");

area = parseInt(length) * parseInt(width);
perimeter = 2*parseInt(length) + 2*parseInt(width);

document.write("Length = " + length + "<br>");
document.write("Width = " + width + "<br>");
document.write("Area = " + area + "<br>");
document.write("Perimeter = " + perimeter +"<br>");
</script>

</body>

</html>

No comments:

Post a Comment