-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework_21_JavaScript.js
More file actions
121 lines (106 loc) · 5.11 KB
/
Homework_21_JavaScript.js
File metadata and controls
121 lines (106 loc) · 5.11 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//////////////////////////////////////////////////////////////////////////////////////
var trigger = false;
while(!trigger){
var numEx = parseInt(prompt("--- Introduceti numarul exercitiului: ---"));
switch (numEx) {
//////////////////////////////////////////////////////////////////////////////////////
// Ex. 1 Functie pentru gasirea celui mai mare numar dintr-un array
case 1:
console.log(`\nEx 1:`);
const initArr = [];
function getNumber() {
var nr = prompt("Introduceti primul nr. din array:");
initArr.push(nr);
return initArr;}
getNumber();
getNumber();
getNumber();
getNumber();
function getMaxArr (arr) {
var maxArr = arr[0];
for (let i = 0; i <= arr.length - 1; i++) {
if (arr[i] > maxArr) {
maxArr = arr[i];}
}
alert(`Valoarea maxima din array-ul [${arr}] este: ${maxArr}`);
console.log("\nEx 1:");
console.log(arr);
console.log(`Valoarea maxima este: ${maxArr}`);
}
getMaxArr(initArr);
break;
//////////////////////////////////////////////////////////////////////////////////////
// Ex. 2 Functie pentru conversia temperaturilor
case 2:
console.log(`\nEx 2:`);
var temp = parseInt(prompt("Introduceti temperatura:"));
var unit = prompt("Introduceti unitatea de masura:");
function convertTemperature (t, u) {
var newTemp = 0;
if (u.toLowerCase() == 'f') {
newTemp = Math.round(((t -32) / 1.8)*10)/10;
alert(`Temperatura ${t}°F este egala cu ${newTemp}°C`);
console.log(`Temperatura ${t}°F este egala cu ${newTemp}°C`);
} else if (u.toLowerCase() == 'c') {
newTemp = Math.round((t * 1.8 + 32)*10)/10;
alert(`Temperatura ${t}°C este egala cu ${newTemp}°F`);
console.log(`Temperatura ${t}°C este egala cu ${newTemp}°F`);}
}
convertTemperature(temp, unit);
//////////////////////////////////////////////////////////////////////////////////////
// Ex. 3 Functie pentru verificarea unui palindrom
case 3:
console.log(`\nEx 3:`);
var userString = prompt("Introduceti un sir de caractere (string):");
console.log(`Sir de char. introdus: ${userString}`);
function isAlphaNumeric (xString) {
let simpleString = [];
for (let i = 0; i < xString.length; i++) {
if ((xString.charCodeAt(i) > 47 && xString.charCodeAt(i) < 58) ||
(xString.charCodeAt(i) > 64 && xString.charCodeAt(i) < 91) ||
(xString.charCodeAt(i) > 96 && xString.charCodeAt(i) < 123)) {
simpleString.push(xString[i]);}}
return simpleString.join('');}
console.log(`Sir de char. alfa-numeric: ${isAlphaNumeric(userString)}`);
function findMiddle (yString) {
let slowPtr = 0;
let fastPtr = 0;
while (yString[fastPtr+1] != null && yString[fastPtr+2] != null) {
slowPtr++;
fastPtr++; fastPtr++;}
return slowPtr;}
function isPalindrome (zString) {
let newString = isAlphaNumeric(zString);
let headPtr = 0;
let tailPtr = -1;
for (let i = 0; i <= findMiddle(newString); i++) {
if (newString[headPtr].toLowerCase() ===
newString.at(tailPtr).toLowerCase()) {
headPtr++; tailPtr--;
} else return false;}
return true;}
if (isPalindrome(userString)) {
alert(`\u2705 Este Palindrom, bravo!`);
console.log(`\u2705 Este Palindrom, bravo!`)}
else {
alert(`\u26D4 Nu este Palindrom!`);
console.log(`\u26D4 Nu este Palindrom.`)}
break;
//////////////////////////////////////////////////////////////////////////////////////
// Ex. 3 Short version
case 4:
console.log(`\nEx 3:`);
function isPalindrom() {
var u = prompt("Introduceti un string:").toLowerCase();
for (let x = 0, y = -1, z = 0; u[z+1] != null && u[z+2] != null ; z++, z++) {
if (u[x] === u.at(y)) {x++; y--;}
else return false;}
return true;}
console.log(isPalindrom());
break;
//////////////////////////////////////////////////////////////////////////////////////
// Exit loop:
case 0:
trigger = true;
break;}}
//////////////////////////////////////////////////////////////////////////////////////