In: Computer Science
Write the below code to use HTML and JavaScript.
1.
a) Write a JavaScript program to display the current day and
time.
b) Write a JavaScript program to print the contents of the current
window.
c) Write a JavaScript program where the program takes a random
integer between 1 to 10
d) Write a JavaScript program to calculate multiplication and
division of two numbers (input from the user).
e)Write a JavaScript program to create a new string from a given
string changing the position of first and last characters. The
string length must be greater than or equal to 1.
f)Write a JavaScript program to check whether a string starts with
'Java' and false otherwise
Answer 1) The answer for subparts is below: I have answered 4 subparts
(A) The HTML and javascript code is below.
<head>
<title>
displaying current date and time
</title>
</head>
<body>
<script>
// creating a date object
var todayDate = new Date();
var todayDay = todayDate.getDay();
// Array of days.
var weekday = ['Sunday', 'Monday', 'Tuesday',
'Wednesday', 'Thursday', 'Friday', 'Saturday'
];
// displaying date and time
document.write("Today is : " + weekday[todayDay]);
document.write("<br/>");
// getting hour from date object
var hours = todayDate.getHours();
var am_pm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
// when we get hours=0 from 12 % 12 = 0 from above then set hour to
12 ie noon 12
hours = hours ? hours : 12;
var minutes = todayDate.getMinutes();
minutes = minutes < 10 ? '0' + minutes : minutes;
var myTime = hours + " " + am_pm + " : " + minutes ;
document.write("\tCurrent time is : " + myTime);
</script>
</body>
</html>
OUTPUT SCREENSHOT
(B) The Code is below:
<!DOCTYPE html>
<html>
<head>
<title>Print the content current window.</title>
</head>
<body>
<p> Window.print() method is used to print the
content.</p>
<p>Click the button to print the current
page.</p>
<button onclick="print_current_page()">Print this
page</button>
<script type="text/javascript">
function print_current_page()
{
window.print();
}
</script>
</body>
</html>
OUTPUT SCREENSHOT
(C) The Code is below:
<html>
<head>
<title>Radom number</title>
</head>
<body>
<script type="text/javascript">
var num = Math.floor(Math.random() * 11);
document.write("The random number is "+ num);
</script>
</body>
</html>
(D) The Code is below.
<html>
<head>
<title>Radom number</title>
</head>
<body>
<!-- form to take input -->
<form onsubmit="return false;">
Enter First number: <br>
<input type="number" id="fnum" name="number1">
<br>
Enter Second number: <br>
<input type="number" id="snum" name="number2"> <br>
<br>
<button onclick="multiply()">multiply</button>
<button onclick="divide()">division</button>
</form>
<h3 id='result'></h3>
<script type="text/javascript">
// function to print multiply
function multiply(){
var num1 = document.getElementById("fnum").value;
var num2 = document.getElementById("snum").value;
document.getElementById('result').innerHTML= "Result of multiply:
"+ num1*num2;
}
// function for division.
function divide(){
var num1 = document.getElementById("fnum").value;
var num2 = document.getElementById("snum").value;
document.getElementById('result').innerHTML= "Result of divide: "+
num1/num2;
}
</script>
</body>
</html>