0% found this document useful (0 votes)
340 views11 pages

Probleme JS

The document contains 10 JavaScript exercises involving functions to: 1. Print the contents of the current window 2. Rotate a string by periodically removing the last letter and adding it to the front 3. Calculate the number of days until Christmas 4. Calculate mathematical operations on user-input numbers 5. Find the smallest prime number greater than a given number 6. Implement bubble sort on an array 7. Get form values and attributes from DOM elements 8. Add rows to a table 9. Calculate the volume of a sphere based on user-input radius

Uploaded by

Someone Ne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
340 views11 pages

Probleme JS

The document contains 10 JavaScript exercises involving functions to: 1. Print the contents of the current window 2. Rotate a string by periodically removing the last letter and adding it to the front 3. Calculate the number of days until Christmas 4. Calculate mathematical operations on user-input numbers 5. Find the smallest prime number greater than a given number 6. Implement bubble sort on an array 7. Get form values and attributes from DOM elements 8. Add rows to a table 9. Calculate the volume of a sphere based on user-input radius

Uploaded by

Someone Ne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

2.Write a JavaScript function to print the contents of the current window.

Rezolvare:

HTML :

<!DOCTYPE html>

<html>

<head>

<meta charset=utf-8 />

<title>This is a thing for printing all content</title>

</head>

<body>

<p> </p>

<p>Click the button to print this page </p>

<button onclick="printeaza()">Press to print!</button>

<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>

<body onload ="animatie_text('target')"

<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

JavaScript Function: Exercise-4


Write a JavaScript function that returns a passed string with letters in alphabetical order.
Example string: 'webmaster'
Expected Output: 'abeemrstw'
function alphabet_order(str)
{
return str.split('').sort().join('');
}
console.log(alphabet_order("webmaster"));

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]

JavaScript DOM: Exercise-2


Write a JavaScript function to get the values of First and Last name of the following form.

<!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);
}
}
}

JavaScript DOM: Exercise-4


Write a JavaScript function to get the value of the href, hreflang, rel, target, and type attributes of the
specified link.

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 DOM: Exercise-5


Write a JavaScript function to add rows to a table.
<!DOCTYPE html>
<html><head>
<meta charset=utf-8 />
<title>Insert row in a table - w3resource</title>
</head><body>
<table id="sampleTable" border="1">
<tr><td>Row1 cell1</td>
<td>Row1 cell2</td></tr>
<tr><td>Row2 cell1</td>
<td>Row2 cell2</td></tr>
</table><br>
<input type="button" onclick="insert_Row()" value="Insert row">
</body></html>

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.

Sample Output of the form :

<!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

You might also like