JavaScript Questions For Practice || JavaScript Practice || Important Question || Practice Set
Q. 1 Write a JavaScript program to display the current day and time in the following
format?
Sample Output : Today is : Tuesday.
Current time is : 10 PM : 30 : 38
// Solutions
// Make a Weeks Array
let weeks=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"] ;
// For Get Current Date
let curdate = new Date() ;
// For Get Current Day
let days = weeks[curdate.getDay()] ;
// For Get Hour
let h = curdate.getHours();
// For Get Minutes
let m = curdate.getMinutes();
// For Get Seconds
let s = curdate.getSeconds();
// First Output
console.log("Today is : " + days) ;
// we use template literls also
console.log(`Today is : ${days}`) ;
// Second Output
console.log("Current time is " + h + " PM "+" : " + m +" : " + s );
// we use template literls also
console.log(`Current time is ${h} PM : ${m} : ${s}`);
Q.2 Write a JavaScript program to print the current window contents ?
solution :-
<body>
<p>Click the button to print the current page.</p>
<button onclick="print_page()">Click For Print</button>
</body>
<script>
function print_page()
{
window.print();
}
</script>
Q.3 Write a JavaScript program to get the current date.
Expected Output :
mm-dd-yyyy, mm/dd/yyyy or dd-mm-yyyy, dd/mm/yyyy
Solution :-
<script>
let d = new Date();
let mm = d.getMonth();
let dd = d.getDay();
let yy = d.getFullYear();
if (mm < 10) {
mm = '0' + mm;
}
if (dd < 10) {
dd = '0' + dd;
}
d = mm + '-' + dd + '-' + yy;
console.log(d);
d = mm + '/' + dd + '/' + yy;
console.log(d);
d = dd + '-' + mm + '-' + yy;
console.log(d);
d = dd + '/' + mm + '/' + yy;
console.log(d);
document.write(date)
</script>
No comments:
Post a Comment