-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletsPlayWithNumbers.html
More file actions
74 lines (71 loc) · 2.22 KB
/
Copy pathletsPlayWithNumbers.html
File metadata and controls
74 lines (71 loc) · 2.22 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Numeri</title>
</head>
<body>
<main>
<h1>
Giochiamo con i numeri Interi (e gli array)!
</h1>
<hr>
<br>
<fieldset>
<legend>Crea la lista di numeri interi lunga quanto vuoi!</legend>
<br>
<label for="max">
Numero massimo nella lista: <br>
</label>
<input type="number" name="" min="0" max="10" id="max" value="0">
<br>
<button id="btn-crea" onclick="mostraArr()">Crea Array</button>
</fieldset>
<div id="array"></div>
<button onclick="trovaPerfetti()">Numeri Perfetti</button>
</main>
</body>
<script>
function mostraArr(){
let t = document.getElementById("array");
let numIn = document.getElementById("max");
let n = creaArr(numIn.value); //genera l'array e lo mette in n (naturali)
t.innerHTML = n.toString();
/*for (let i = 0; i < n.length; i++){
console.log('array ', i, 'valore ', n[i])
}*/
}
function creaArr(maxVal){
// console.log(maxVal);
let arr = [];
for(let i = 0; i <= maxVal; i++){
arr.push(i);
}
return arr;
}
function arrFromHt(idDiv){
return document.getElementById(idDiv).innerHTML.split(',');
}
function trovaPerfetti(){
/*trova i divisori e li somma*/
let arr = arrFromHt("array");
//console.log('stringa array', strArr);
let perfetti = [];
for(let i = 0; i < arr.length; i++){
let sommaDivisori = 0;
for (let j = 0; j < arr[i]; j++){
//console.log("controllo se il numero ", arr[i], " è divisibile per ", j);
if(arr[i] % j == 0){
sommaDivisori += j;
}
if(sommaDivisori == arr[i]){
console.log("somma Divisori", sommaDivisori, "\nNumero controllato", arr[i])
}
}
}
return perfetti;
}
</script>
</html>