Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions web-app/js/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -1534,22 +1534,36 @@ function initFibonacci() {
const ctx = canvas.getContext('2d');

function generateFibonacci() {
const n = parseInt(termsInput.value) || 10;
const value = termsInput.value.trim();
const n = parseInt(value);

// Validation
if (value === '' || isNaN(n) || n <= 0) {
display.innerHTML = `
<p class="fib-error">
Please enter a number greater than 0
</p>
`;

ctx.clearRect(0, 0, canvas.width, canvas.height);
return;
}

display.innerHTML = '';

let fib = [0, 1];
for (let i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
fib[i] = fib[i - 1] + fib[i - 2];
}

fib.slice(0, n).forEach((num, index) => {
const numEl = document.createElement('div');
numEl.className = 'fib-number';
numEl.textContent = num;
numEl.style.animationDelay = `${index * 0.1}s`;
display.appendChild(numEl);
});

drawSpiral(fib.slice(0, Math.min(n, 12)));
}

Expand Down
31 changes: 25 additions & 6 deletions web-app/js/projects/fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ function getFibonacciHTML() {
#fibTerms{
padding:13px;
background-color:var(--bg-color);
border:1px solid white;
color: var(--text-color);
border:1px solid var(--text-color);
outline:none;
border-radius:30px;
}
Expand All @@ -61,6 +62,11 @@ function getFibonacciHTML() {
align-items:center;
justify-content:center;
}
.fib-error{
color:red;
font-weight:bold;
margin-top:1rem;
}
</style>
`;
}
Expand All @@ -73,22 +79,35 @@ function initFibonacci() {
const ctx = canvas.getContext('2d');

function generateFibonacci() {
const n = parseInt(termsInput.value) || 10;
const value = termsInput.value.trim();
const n = parseInt(value);

// Validation
if (value === '' || isNaN(n) || n <= 0) {
display.innerHTML = `
<p class="fib-error">
Please enter a number greater than 0
</p>
`;
ctx.clearRect(0, 0, canvas.width, canvas.height);
return;
}

display.innerHTML = '';

let fib = [0, 1];
for (let i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
fib[i] = fib[i - 1] + fib[i - 2];
}

fib.slice(0, n).forEach((num, index) => {
const numEl = document.createElement('div');
numEl.className = 'fib-number';
numEl.textContent = num;
numEl.style.animationDelay = `${index * 0.1}s`;
display.appendChild(numEl);
});

drawSpiral(fib.slice(0, Math.min(n, 12)));
}

Expand Down
Loading