-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework_20_JavaScript.js
More file actions
57 lines (53 loc) · 2.42 KB
/
Homework_20_JavaScript.js
File metadata and controls
57 lines (53 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//////////////////////////////////////////////////////////////////////////////////////
var trigger = true;
while(trigger){
var numEx = parseInt(prompt("--- Introduceti numarul exercitiului: ---"));
switch (numEx) {
//////////////////////////////////////////////////////////////////////////////////////
// Ex. 1 - Suma elementelor dintr-un array.
case 1:
const numbers = [1, 2, 3, 4, 5];
var sumArr = 0;
for (let i = 0; i <= numbers.length - 1; i++) {
sumArr += numbers[i];
}
alert(`Suma elementelor array-ului [${numbers}] este: ${sumArr}`);
console.log("\nEx 1:");
console.log(numbers);
console.log(`Suma este: ${sumArr}`);
break;
//////////////////////////////////////////////////////////////////////////////////////
// Ex. 2 - Gasirea valorii maximale dintr-un array.
case 2:
const numere = [10, 20, 5, 25, 30];
var maxArr = null;
for (let i = 0; i <= numere.length - 1; i++) {
if (numere[i] > maxArr) {
maxArr = numere[i];}
}
alert(`Valoarea maxima din array-ul [${numere}] este: ${maxArr}`);
console.log("\nEx 2:");
console.log(numere);
console.log(`Valoarea maxima este: ${maxArr}`);
break;
//////////////////////////////////////////////////////////////////////////////////////
// Ex. 3 - Parcurgerea unui tablou multidimensional.
case 3:
const nestedArray = [[1, 2], [3, 4, 5], [6, 7, 8, 9]];
var elem = [];
console.log("\nEx 3:");0
console.log(nestedArray);
for (let i = 0; i <= nestedArray.length - 1; i++) {
console.log(`\n[Array ${i+1}]`);
for (let j = 0; j <= nestedArray[i].length -1; j++) {
console.log(`Elem. array: ${nestedArray[i][j]}`);
elem.push(` ${nestedArray[i][j]}`);}
}
alert(`Elem. array-ului [[1, 2],[3, 4, 5],[6, 7, 8, 9]] sunt:\n ${elem}`);
break;
//////////////////////////////////////////////////////////////////////////////////////
// Exit loop:
case 0:
trigger = false;
break;}}
//////////////////////////////////////////////////////////////////////////////////////