Académique Documents
Professionnel Documents
Culture Documents
Rezolvare:
HTML :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> </p>
<script src="printing.js"></script>
</body>
</html>
Javascript(js):
function printeaza()
window.print();
Exercise-5
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.
HTML Code:
<!DOCTYPE html>
1
<html>
<head>
<meta charset="utf-8"/>
<title>INCROYABLE</title>
</head>
<pre id="target">wr3resource</pre>
<script src="animatie.js"></script>
</body>
</html>
ES6 Version:
function animatie_text(id) {
const element=document.getElementById(id);
const textNode=element.childNodes[0];
let text=textNode.data;
setInterval(() => {
text=text[text.length-1]+text.substring(0,text.length-1);
textNode.data=text;
}, 100);
Exercise-9
Write a JavaScript program to calculate number of days left until next Christmas.
const today = new Date();
let christmas=new Date(today.getFullYear(),11,25);
2
if ((today.getMonth()>=11)&&(today.getDay()>25))
christmas.setFullYear(christmas.getFullYear()+1);
durationDay=1000*24*60*60;
console.log(`${Math.ceil((christmas.getTime()-today.getTime())/durationDay)} days left until
Christmas day`);
Exercise-10
Write a JavaScript program to calculate multiplication and division of two numbers (input from
user).
Sample Form:
HTML Code:
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
<title> Double incroyable</title>
</head>
<body>
<form>
1st Number: <input type="text" id="firstNumber"/><br><br>
3
2nd Number:<input type="text" id="secondNumber"/><br><br>
<input type ="button" onclick="multiplyBy()" value="Multiply" />
<input type="button" onclick="divideBy()" value="Divide" />
</form>
<p> The result is :
<span id="result"> </span>
</p>
<script src="calculate.js"></script>
</body>
</html>
ES6 Version:
function multiplyBy(){
number1=document.getElementById('firstNumber').value;
number2=document.getElementById('secondNumber').value;
result = number1*number2;
document.getElementById('result').innerHTML=result;
}
function divideBy() {
number1=document.getElementById('firstNumber').value;
number2=document.getElementById('secondNumber').value;
result = number1/number2;
document.getElementById('result').innerHTML=result;
}
4
Exercise-129
Write a JavaScript program to find the smallest prime number strictly greater than a given
number.
function number_prime(number){
for(var i=number+1;;i++){
var prime=true;
for(var d=2;d*d<=i;d++){
if(i%d==0){
prime=false;
break;
}
}
if(prime)
return i;
}
}
console.log(number_prime(8)); //outputs 11
console.log(number_prime(32)); //outputs 37
5
JavaScript fundamental (ES6 Syntax): Exercise-24
const decapitalize = ([first, ...rest], upperRest = false) =>
first.toLowerCase() + (upperRest ? rest.join('').toUpperCase() : rest.join(''));
console.log(decapitalize('W3resource'))
console.log(decapitalize('Red', true));
Output:
w3resource
rED
Sample Output:
abeemrstw
Explanation:
The split() method is used to split a String object into an array of strings by separating the string
into substrings.
Code : console.log('32243'.split(""));
Output : ["3", "2", "2", "4", "3"]
The sort() method is used to sort the elements of an array in place and returns the array.
Code : console.log(["3", "2", "2", "4", "3"].sort());
Output : ["2", "2", "3", "3", "4"]
The join() method is used to join all elements of an array into a string.
Code : console.log(["2", "2", "3", "3", "4"].join(""));
Output : "22334"
6
JavaScript Object: Exercise-6
Write a Bubble Sort algorithm in JavaScript.
Note: Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be
sorted,
Sample Data: [6,4,0, 3,-2,1]
Expected Output : [-2, 0, 1, 3, 4, 6]
Array.prototype.bubbleSort_algo = function()
{
var is_sorted = false;
while (!is_sorted)
{
is_sorted = true;
for (var n = 0; n < this.length - 1; n++)
{
if (this[n] > this[n+1]){
var x = this[n+1];
this[n+1] = this[n];
this[n] = x;
is_sorted = false;
}
}
}
return this;
};
console.log([6,4,0, 3,-2,1].bubbleSort_algo());
Sample Output:
[-2,0,1,3,4,6]
<!DOCTYPE html>
<html><head>
<meta charset=utf-8 />
<title>Return first and last name from a form -
w3resource</title>
</head><body>
<form id="form1" onsubmit="getFormvalue()">
7
First name: <input type="text" name="fname" value="David"><br>
Last name: <input type="text" name="lname" value="Beckham"><br>
<input type="submit" value="Submit">
</form>
</body>
</html
function getFormvalue()
{
var x=document.getElementById("form1");
for (var i=0;i<x.length;i++)
{
if (x.elements[i].value!='Submit')
{
console.log(x.elements[i].value);
}
}
}
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Collect the value of href, hreflang, rel, target, and type
attributes of a link</title>
</head>
<body>
<p><a id="w3r" type="text/html" hreflang="en-us" rel="nofollow"
target="_self" href="https://www.w3resource.com/">w3resource</a></p>
<button onclick="getAttributes()">Click here to get the attribute's
value</button>
</body>
</html>
8
JavaScript Code:
function getAttributes()
{
var u = document.getElementById("w3r").href;
alert('The value of the href attribute of the link is : '+u);
var v = document.getElementById("w3r").hreflang;
alert('The value of the hreflang attribute of the link is : '+v);
var w = document.getElementById("w3r").rel;
alert('The value of the rel attribute of the link is : '+w);
var x = document.getElementById("w3r").target;
alert('The value of the taget attribute of the link is : '+x);
var y = document.getElementById("w3r").type;
alert('The value of the type attribute of the link is : '+y);
}
JavaScript Code:
function insert_Row()
{
var x=document.getElementById('sampleTable').insertRow(0);
var y = x.insertCell(0);
var z = x.insertCell(1);
y.innerHTML="New Cell1";
z.innerHTML="New Cell2";
}
9
JavaScript DOM: Exercise-10
Write a JavaScript program to calculate the volume of a sphere.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Volume of a Sphere</title>
</head>
<body>
<p>Input radius value and get the volume of a sphere.</p>
<form action="" method="post" id="MyForm">
<label for="radius">Radius </label><input type="text" name="radius" id="radius" required>
<br>
<br>
<label for="volume">Volume</label><input type="text" name="volume" id="volume">
<br>
<br>
<input type="submit" value="Calculate" id="submit"> </form>
10
<script src="volume.js">
</script
</body>
JavaScript Code:
function volume_sphere()
{
var volume;
var radius = document.getElementById('radius').value;
radius = Math.abs(radius);
volume = (4/3) * Math.PI * Math.pow(radius, 3);
volume = volume.toFixed(4);
document.getElementById('volume').value = volume;
return false;
}
window.onload = document.getElementById('MyForm').onsubmit =
volume_sphere;
11