-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
76 lines (67 loc) · 1.71 KB
/
script.js
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
document.getElementById('calculateButton').addEventListener('click', function() {
const input = document.getElementById('fibonacciInput').value;
const result = document.getElementById('result');
const chartCanvas = document.getElementById('fibonacciChart');
const numberInput = Number(input);
if (isNaN(numberInput) || !Number.isInteger(numberInput) || numberInput < 0) {
result.textContent = "Please enter a valid non-negative integer.";
return;
}
const fibonacci = (num) => {
let sequence = [0, 1];
for (let i = 2; i <= num; i++) {
sequence.push(sequence[i - 1] + sequence[i - 2]);
}
return sequence;
};
const fibSequence = fibonacci(numberInput);
if (!Array.isArray(fibSequence) || fibSequence.length === 0) {
result.textContent = "Error calculating the Fibonacci sequence.";
return;
}
result.textContent = `Fibonacci(${numberInput}) = ${fibSequence[numberInput]}`;
// Konfiguracja wykresu
const labels = [...Array(fibSequence.length).keys()];
const data = {
labels: labels,
datasets: [{
label: 'Fibonacci Sequence',
backgroundColor: '#1e90ff',
borderColor: '#4682b4',
data: fibSequence,
fill: false,
}]
};
const config = {
type: 'line',
data: data,
options: {
responsive: true,
scales: {
x: {
title: {
display: true,
text: 'n-th Fibonacci Number'
}
},
y: {
title: {
display: true,
text: 'Value'
},
beginAtZero: true
}
}
}
};
// Usunięcie starego wykresu, jeśli istnieje
if (window.fibChart) {
window.fibChart.destroy();
}
// Stworzenie nowego wykresu, jeśli dane są poprawne
if (fibSequence.every(num => Number.isFinite(num))) {
window.fibChart = new Chart(chartCanvas, config);
} else {
result.textContent = "Invalid data for chart generation.";
}
});