Sei sulla pagina 1di 5

1. Write a JavaScript program to print the contents of the current window.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Print the current page.</title>
</head>
<body>
<p></p>
<p>Click the button to print the current page.</p>
<button onclick="print_current_page()">Print this page</button>
</body>
</html>

function print_current_page()
{
window.print();
}

window.print(): The window object represents a window containing a DOM document;


the document property points to the DOM document loaded in that window,
window.print() is used to open the Print Dialog to print the current document.

2. Write a JavaScript program to get the current date.

var today = new Date();


var dd = today.getDate();

var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}

if(mm<10)
{
mm='0'+mm;
}
today = mm+'-'+dd+'-'+yyyy;
console.log(today);
today = mm+'/'+dd+'/'+yyyy;
console.log(today);
today = dd+'-'+mm+'-'+yyyy;
console.log(today);

3. Write a JavaScript function to find the area of a triangle where lengths of the
three of its sides are 5, 6, 7.

var side1 = 5;
var side2 = 6;
var side3 = 7;
var s = (side1 + side2 + side3)/2;
var area = Math.sqrt(s*((s-side1)*(s-side2)*(s-side3)));
console.log(area);

4. Write a JavaScript program to rotate the string 'w3resource' in right direction


by periodically removing one letter from the end of the string and attaching it to
the front.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript basic animation</title>
<script type="text/javascript">
</script>
</head> <body onload="animate_string('target')"
<pre id="target">w3resource </pre>
</body>
</html>

function animate_string(id)
{
var element = document.getElementById(id);
var textNode = element.childNodes[0]; // assuming no other children
var text = textNode.data;

setInterval(function ()
{
text = text[text.length - 1] + text.substring(0, text.length - 1);
textNode.data = text;
}, 100);
}

5. Write a JavaScript program to determine whether a given year is a leap year in


the Gregorian calendar.

function leapyear(year)
{
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}
console.log(leapyear(2016));
console.log(leapyear(2000));
console.log(leapyear(1700));
console.log(leapyear(1800));

6. Write a JavaScript program to find which 1st January is being a Sunday between
2014 and 2050.

console.log('--------------------');
for (var year = 2014; year <= 2050; year++)
{
var d = new Date(year, 0, 1); //0 for sunday, 1 for monday,...
if ( d.getDay() === 0 )
console.log("1st January is being a Sunday "+year);
}
console.log('--------------------');

6. Write a JavaScript program where the program takes a random integer between 1 to
10, the user is then prompted to input a guess number. If the user input matches
with guess number, the program will display a message "Good Work" otherwise display
a message "Not matched".

console.log(leapyear(100));

JavaScript Code:
// Get a random integer from 1 to 10 inclusive
const num = Math.ceil(Math.random() * 10);
console.log(num);
const gnum = prompt('Guess the number between 1 and 10 inclusive');
if (gnum == num)
console.log('Matched');
else
console.log('Not matched, the number was '+gnum);

7. Write a JavaScript program to calculate days left until next Christmas

today=new Date();
var cmas=new Date(today.getFullYear(), 11, 25);
if (today.getMonth()==11 && today.getDate()>25)
{
cmas.setFullYear(cmas.getFullYear()+1);
}
var one_day=1000*60*60*24;
console.log(Math.ceil((cmas.getTime()-today.getTime())/(one_day))+
" days left until Christmas!");

8. Write a JavaScript program to calculate multiplication and division of two


numbers (input from user).

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JavaScript program to calculate multiplication and division of two numbers
</title>
<style type="text/css">
body {margin: 30px;}
</style>
</head>
<body>
<form>
1st Number : <input type="text" id="firstNumber" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<input type="button" onClick="multiplyBy()" Value="Multiply" />
<input type="button" onClick="divideBy()" Value="Divide" />
</form>
<p>The Result is : <br>
<span id = "result"></span>
</p>
</body>
</html>

<script>
function multiplyBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 * num2;
}

function divideBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 / num2; //
document.getElementById("result").innerHTM : The innerHTML property sets or returns
the HTML content (inner HTML) of an element.

9. Convert temperatures to and from celsius, fahrenheit

C = (5/9) * (F - 32)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Write a JavaScript program to convert temperatures to and from celsius,
fahrenheit</title>
</head>
<body>
</body>
</html>

function cToF(celsius)
{
var cTemp = celsius;
var cToFahr = cTemp * 9 / 5 + 32;
var message = cTemp+'\xB0C is ' + cToFahr + ' \xB0F.';
console.log(message);
}

function fToC(fahrenheit)
{
var fTemp = fahrenheit;
var fToCel = (fTemp - 32) * 5 / 9;
var message = fTemp+'\xB0F is ' + fToCel + '\xB0C.';
console.log(message);
}
cToF(60);
fToC(45);

}
</script>
10. Write a JavaScript program to get the website URL (loading page).
console.log(document.URL);

The URL read-only property of the Document interface returns the document location
as a string.

11. Create a variable using a user-defined name

var var_name = 'abcd';


var n = 120;
this[var_name] = n;

12. Write a JavaScript exercise to get the extension of a filename.

filename = "abc.js"
console.log(filename.split('.').pop());
console.log(this[var_name])

Potrebbero piacerti anche