0% found this document useful (0 votes)
84 views

Culegere de Exemple - Java Script

The document contains 10 examples of JavaScript code demonstrating the use of conditional statements (if/else), loops (for, while), functions, arrays, and matrices. The examples show how to: 1) Use if/else statements to check conditions and display different messages. 2) Implement a switch-case statement to check different cases and display corresponding messages. 3) Define and call functions to perform calculations like summing numbers in a range. 4) Create and manipulate arrays and matrices to store and display multiple values. 5) Use for and while loops to iterate through elements and perform calculations. 6) Add buttons to trigger functions on click that calculate maximum values or averages.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Culegere de Exemple - Java Script

The document contains 10 examples of JavaScript code demonstrating the use of conditional statements (if/else), loops (for, while), functions, arrays, and matrices. The examples show how to: 1) Use if/else statements to check conditions and display different messages. 2) Implement a switch-case statement to check different cases and display corresponding messages. 3) Define and call functions to perform calculations like summing numbers in a range. 4) Create and manipulate arrays and matrices to store and display multiple values. 5) Use for and while loops to iterate through elements and perform calculations. 6) Add buttons to trigger functions on click that calculate maximum values or averages.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CULEGERE DE EXEMPLE –JAVA SCRIPT

EXEMPLUL 1. IF ..ELSE

<html>
<body>
<script language='javascript' type='text/javascript'>
a=13;b=2;
if(a>b){
alert('a este mai mare ca b');
}
</script>
</body>
</html>

EXEMPLUL 2. IF .. ELSE

<html>
<head></head>
<body>
<script language="javascript">

var nota;
nota=prompt("Introduceti nota","0");
if (nota<5)
{
alert ("Nepromovat");
}
else
{

if (nota<7)
{
alert ("Bine");
}
else
{
alert ("Foarte bine");
}

</script>
</body>
</html>
EXEMPLUL 3. SWITCH-CASE

<html>
<body>
<script language='javascript' type='text/javascript'>
var oras='Ploiesti';
switch(oras){
case 'Ploiesti':
document.write('Locuiti la Ploiesti');
break;
case 'Peris':
document.write('Locuiti la Peris');
break;
case 'Paris':
document.write('Locuiti la Paris');
break;
case 'Roman':
document.write('Locuiti la Roman');
break;
case 'Roma':
document.write('Locuiti la Roma');
break;
default:
document.write('Sunteti un itinerant!');
document.write('La revedere!');
}
</script>
</body>
</html>

EXEMPLUL 4. SWITCH-CASE fara repetarea operatiilor


<html>
<body>
<script language='javascript' type='text/javascript'>
var numar;
numar = parseInt(prompt("introduceti un numar intreg = 10,20,30"));
switch(numar) {
case 10:
document.write('10');
break;
case 20:
document.write('20');
break;
case 30:
document.write('30');
break;
default:
document.write('Alta valoare');
document.write('<br />La revedere!');
}
</script>
</body>
</html>

EXEMPLUL 4. SWITCH-CASE
cu repetarea operatiilor de un anumit numar de ori (DO WHILE)

<html>
<body>
<script language='javascript' type='text/javascript'>
var numar;
R='DA';
do {
numar = parseInt(prompt("introduceti un numar intreg = 10,20,30"));
switch(numar) {
case 10:
document.write('10'+'<br />');
break;
case 20:
document.write('20'+'<br />');
break;
case 30:
document.write('30'+'<br />');
break;
default:
document.write('Alta valoare');
document.write('<br />La revedere!');
}
R = prompt('introduceti DA sau NU', ' ');
}
while(R=='DA');
</script>
</body>
</html>
EXEMPLUl 5 - FUNCTION

<html>
<head>
<script language='javascript'>
function suma(n) {
var i=Number(0);
var suma=Number(0);
do
{
suma=suma+i;
i++; //i=i+1
}
while (i<=n);
alert('Suma este: ' +suma);
}
</script>
</head>
<body>
<script language='javascript'>
var n;
n=prompt('Pentru cate numere calculati suma?');
rezultat=suma(n);
</script>
</body>
</html>

EXEMPLUL 6. VECTOR
<html>
<head>
<script language='javascript'>
ListaCulori=new Array('rosu','galben','albastru','verde');
document.write(ListaCulori+'<br />');
//Se afiseaza rosu, galben, albastru, verde
ListaCulori[1]='violet';
document.write(ListaCulori);
//Se afiseaza rosu, violet, albastru, verde
</script>
</html>

EXEMPLUL 7. MATRICE

<html>
<body>
<script language='javascript' type='text/javascript'>
//creare matrice cu trei linii si patru coloane
linie=new Array(3);
linie[0]=new Array('a11','a12','a13','a14');
linie[1]=new Array('a21','a22','a23','a24');
linie[2]=new Array('a31','a32','a33','a34');
document.write(linie[1][2]+'<br>');
//Se afiseaza a23
document.write(linie[0]);
//Se afiseaza a11, a12, a13, a14
</script>
</body>
</html>

EXEMPLUL 8. Afisarea elementelor unei matrici cu o bucla for


<html>
<head>
<script language='javascript'>
/*Afisarea elementelor unei matrici cu o bucla for*/
student=new Array('Alin','Bogdan','Catalin','Dan');
for(i=0;i<4;i++){
document.write(student[i]+'<br>');
}
</script>
</head>
</html>
EXEMPLUL 8.1. Afisarea elementelor unei matrici cu o bucla for
si apoi inversarea ordinei lor

<html>
<head>
<script language='javascript'>
/*Afisarea elementelor unei matrici cu o bucla for*/
student=new Array('Alin','Bogdan','Catalin','Dan');
for(i=0;i<4;i++){
document.write(student[i]+'<br>');
}
matrice_inversa=student.reverse()
document.write(student);
</script>
</head>
</html>

EXEMPLUL 9. WHILE

<html>
<body>
<script language='javascript'>
var n=Number(0);
var i=Number(0);
var suma=Number(0);
n=prompt('Pentru cate numere calculati suma');
while (i<=n)
{
suma=suma+i;
i++;
}
alert('Suma este: ' +suma);
</script>
</body>
</html>

EXEMPLUL 10. Problema complexa cu evenimentul onClick

<html>
<head>
<script type="text/javascript">
function CalculMaxim()
{
var a,b,c;
a = parseInt(prompt("introduceti a"));
b = parseInt(prompt("introduceti b"));
c = parseInt(prompt("introduceti c"));
if (a>b)
if (a>c)
alert("Maximul este " + a);
else
alert("Maximul este " + c);
else
if (b>c)
alert("Maximul este " + b);
else
alert("Maximul este " + c);
}

function Medie_For()
{
alert(navigator.appName);
var suma, nr, i;
nr = prompt("introduceti numarul de elemente");
suma = 0;
for(i=1; i<=nr; i++)
suma = suma + parseInt(prompt("introduceti elementul " + i));
if (nr>0)
alert("Media este " + suma/nr);
else
alert("Media nu se poate calcula");
}
function Medie_While()
{
var suma, nr, i;
nr = prompt("introduceti numarul de elemente");
suma = 0;
i = 1;
while(i<=nr)
{
suma = suma + parseInt(prompt("introduceti elementul " + i));

i++;
}
if (nr>0)
alert("Media este " + suma/nr);
else
alert("Media nu se poate calcula");
}
</script>
</head>

<body>
<form>
<input type="button" onClick="CalculMaxim()" value="Calcul Maxim">
<input type="button" onClick="Medie_For()" value="Calcul Medie FOR">
<input type="button" onClick="Medie_While()" value="Calcul Medie WHILE">
</form
</body>
</html>

You might also like