Skip to content

Commit 7c24b9a

Browse files
committed
fix:most projects are functioning properly
1 parent eebc6f6 commit 7c24b9a

14 files changed

Lines changed: 1211 additions & 1540 deletions

web-app/js/projects/armstrong.js

Lines changed: 203 additions & 228 deletions
Large diffs are not rendered by default.

web-app/js/projects/binary-search.js

Lines changed: 194 additions & 129 deletions
Large diffs are not rendered by default.

web-app/js/projects/bubble-sort.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@ function getBubbleSortHTML() {
207207
height: 14px;
208208
border-radius: 3px;
209209
}
210+
211+
.bar {
212+
display: flex !important;
213+
flex-direction: column !important;
214+
align-items: center !important;
215+
justify-content: flex-end !important;
216+
border-radius: 6px 6px 0 0 !important;
217+
background: linear-gradient(180deg, #667eea, #764ba2) !important;
218+
position: relative !important;
219+
transition: all 0.2s ease !important;
220+
}
221+
222+
.bar-label {
223+
color: white !important;
224+
font-size: 0.8rem !important;
225+
font-weight: bold !important;
226+
margin-bottom: 6px !important;
227+
}
210228
</style>
211229
`;
212230
}
@@ -260,22 +278,40 @@ function initBubbleSort() {
260278
}
261279

262280
function renderBars(arr, comparing = [], swapping = [], sorted = []) {
263-
const maxVal = Math.max(...arr);
264-
barsDiv.textContent = arr.map((val, i) => {
265-
const heightPct = Math.max(15, (val / maxVal) * 180);
266-
let cls = 'bar';
267-
if (swapping.includes(i)) cls += ' swapping';
268-
else if (comparing.includes(i)) cls += ' comparing';
269-
else if (sorted.includes(i)) cls += ' sorted';
270-
return `<div class="${cls}" style="height:${heightPct}px">
271-
<span class="bar-label">${val}</span>
272-
</div>`;
273-
}).join('');
274-
275-
statsDiv.textContent = `
276-
<div class="stat-item">🔍 Comparisons: <span>${comparisons}</span></div>
277-
<div class="stat-item">🔄 Swaps: <span>${swaps}</span></div>
278-
`;
281+
if (!barsDiv) return;
282+
283+
const maxVal = Math.max(...arr, 1);
284+
barsDiv.innerHTML = '';
285+
286+
arr.forEach((val, i) => {
287+
const heightPct = Math.max(25, (val / maxVal) * 180);
288+
const bar = document.createElement('div');
289+
bar.className = 'bar';
290+
bar.style.height = heightPct + 'px';
291+
bar.style.width = '50px';
292+
293+
if (swapping.includes(i)) {
294+
bar.classList.add('swapping');
295+
} else if (comparing.includes(i)) {
296+
bar.classList.add('comparing');
297+
} else if (sorted.includes(i)) {
298+
bar.classList.add('sorted');
299+
}
300+
301+
const label = document.createElement('span');
302+
label.className = 'bar-label';
303+
label.textContent = val;
304+
bar.appendChild(label);
305+
306+
barsDiv.appendChild(bar);
307+
});
308+
309+
if (statsDiv) {
310+
statsDiv.innerHTML = `
311+
<div class="stat-item">🔍 Comparisons: <span>${comparisons}</span></div>
312+
<div class="stat-item">🔄 Swaps: <span>${swaps}</span></div>
313+
`;
314+
}
279315
}
280316

281317
async function bubbleSortVisualize(arr) {

web-app/js/projects/collatz.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function initCollatz() {
152152

153153
if (!number || number < 1) {
154154
sequenceDiv.textContent = '<p style="color: var(--danger-color);">⚠️ Please enter a positive integer!</p>';
155-
statsDiv.textContent = '';
155+
statsDiv.innerHTML = '';
156156
ctx.clearRect(0, 0, canvas.width, canvas.height);
157157
return;
158158
}
@@ -177,7 +177,7 @@ function initCollatz() {
177177
const maxNum = Math.max(...sequence);
178178
const statusText = reachedOne ? 'Reached 1 ✅' : `Not reached in ${maxSteps} steps ❌`;
179179

180-
statsDiv.textContent = `
180+
statsDiv.innerHTML = `
181181
<div class="stat-box">
182182
<div class="stat-label">Starting Number</div>
183183
<div class="stat-value">${originalNumber}</div>
@@ -196,9 +196,9 @@ function initCollatz() {
196196
</div>
197197
`;
198198

199-
sequenceDiv.textContent = reachedOne
200-
? '<p style="margin-bottom: 1rem; color: var(--success-color); font-weight: 600;">✅ This number reaches 1.</p>'
201-
: `<p style="margin-bottom: 1rem; color: var(--warning-color); font-weight: 600;">⚠️ Could not confirm reach to 1 within ${maxSteps} steps.</p>`;
199+
sequenceDiv.innerHTML = reachedOne
200+
? '<p style="margin-bottom: 1rem; color: var(--success-color); font-weight: 600;">✅ This number reaches 1.</p>'
201+
: '<p style="margin-bottom: 1rem; color: var(--warning-color); font-weight: 600;">⚠️ Could not confirm reach to 1 within 20000 steps.</p>';
202202
sequence.forEach((num, index) => {
203203
const numEl = document.createElement('span');
204204
numEl.className = 'sequence-number';

web-app/js/projects/color-palette.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ function initColorPalette() {
703703
palette.colors.forEach(color => {
704704
const swatch = document.createElement('div');
705705
swatch.className = 'cp-swatch';
706-
swatch.textContent = `
706+
swatch.innerHTML = `
707707
<div class="cp-swatch-color" style="background:${color.hex};"></div>
708708
<div class="cp-swatch-info">
709709
<span class="cp-swatch-name">${color.name}</span>

web-app/js/projects/dots-boxes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ function initDotsBoxes() {
789789

790790
const overlay = document.createElement('div');
791791
overlay.className = 'winner-overlay';
792-
overlay.textContent = `
792+
overlay.innerHTML = `
793793
<div class="winner-popup" role="dialog" aria-modal="true" aria-label="Game over">
794794
<h2>${title}</h2>
795795
<div class="winner-subtitle">${subtitle}</div>

web-app/js/projects/math-quiz.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ function initMathQuiz() {
455455

456456
const correctIdx = options.indexOf(correct);
457457

458-
board.textContent = `<p class="quiz-question">❓ ${question}</p>`;
458+
board.innerHTML = `<p class="quiz-question">❓ ${question}</p>`;
459459
optWrap.textContent = '';
460460
msgEl.textContent = '';
461461
total += 10;
@@ -532,7 +532,7 @@ options.forEach((opt, i) => {
532532
timerEl.style.color = '#ff3b30';
533533
gameRunning = false;
534534
optWrap.textContent = '';
535-
board.textContent = `
535+
board.innerHTML = `
536536
<div>
537537
<p class="quiz-gameover">💀 Game Over!</p>
538538
<p>⭐ Score: ${score}</p>
@@ -563,7 +563,7 @@ options.forEach((opt, i) => {
563563
timerEl.style.color = '#34c759';
564564
resetState();
565565
updateHUD();
566-
board.textContent = '<p class="quiz-start-msg">Press Start to Play! 🎮</p>';
566+
board.innerHTML = '<p class="quiz-start-msg">Press Start to Play! 🎮</p>';
567567
optWrap.textContent = '';
568568
msgEl.textContent = '';
569569
startBtn.textContent = '▶️ Start';

web-app/js/projects/morse-code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function initMorseCode() {
220220
if (char !== ' ') {
221221
const item = document.createElement('div');
222222
item.className = 'chart-item';
223-
item.textContent = `
223+
item.innerHTML = `
224224
<div class="chart-char">${char}</div>
225225
<div class="chart-morse">${morseCode[char]}</div>
226226
`;

web-app/js/projects/number-guessing.js

Lines changed: 96 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -275,92 +275,117 @@ function initNumberGuessing() {
275275
});
276276

277277
function startGame(diffKey) {
278-
currentDiff = diffKey;
279-
const config = DIFFICULTIES[diffKey];
280-
maxAttempts = config.attempts;
281-
minRange = config.min;
282-
maxRange = config.max;
283-
secretNumber = Math.floor(Math.random() * (config.max - config.min + 1)) + config.min;
284-
attempts = 0;
285-
286-
// Badge
287-
difficultyBadge.textContent = config.label;
288-
difficultyBadge.className = `difficulty-badge ${diffKey}`;
289-
290-
// Instruction
291-
gameInstructions.textContent =
292-
`I'm thinking of a number between ${config.min} and ${config.max}!`;
293-
294-
// Input bounds
295-
guessInput.min = config.min;
296-
guessInput.max = config.max;
297-
guessInput.value = '';
298-
299-
// Stats
300-
attemptsLeftEl.textContent = maxAttempts;
301-
rangeDisplay.textContent = `${config.min}${config.max}`;
302-
feedback.textContent = '';
303-
feedback.style.color = '';
304-
305-
updateBar(maxAttempts, maxAttempts);
306-
updateBestScore(diffKey);
307-
308-
guessInput.disabled = false;
309-
submitBtn.disabled = false;
310-
311-
difficultyScreen.style.display = 'none';
312-
gameScreen.style.display = '';
313-
guessInput.focus();
314-
}
278+
currentDiff = diffKey;
279+
const config = DIFFICULTIES[diffKey];
280+
maxAttempts = config.attempts;
281+
secretNumber = Math.floor(Math.random() * (config.max - config.min + 1)) + config.min;
282+
attempts = 0;
283+
minRange = config.min;
284+
maxRange = config.max;
285+
guessInput.min = minRange;
286+
guessInput.max = maxRange;
287+
288+
// Badge
289+
difficultyBadge.textContent = config.label;
290+
difficultyBadge.className = `difficulty-badge ${diffKey}`;
291+
292+
// Instruction
293+
gameInstructions.textContent =
294+
`I'm thinking of a number between ${config.min} and ${config.max}!`;
295+
296+
// Input bounds - IMPORTANT!
297+
guessInput.min = config.min;
298+
guessInput.max = config.max;
299+
guessInput.value = '';
300+
301+
// Stats
302+
attemptsLeftEl.textContent = maxAttempts;
303+
rangeDisplay.textContent = `${config.min}${config.max}`;
304+
feedback.textContent = '';
305+
feedback.style.color = '';
306+
307+
updateBar(maxAttempts, maxAttempts);
308+
updateBestScore(diffKey);
309+
310+
guessInput.disabled = false;
311+
submitBtn.disabled = false;
312+
313+
difficultyScreen.style.display = 'none';
314+
gameScreen.style.display = '';
315+
guessInput.focus();
316+
}
315317

316318
// Gameplay
317319
submitBtn.addEventListener('click', makeGuess);
318320
guessInput.addEventListener('keydown', e => { if (e.key === 'Enter') makeGuess(); });
319321

320322
function makeGuess() {
321-
const config = DIFFICULTIES[currentDiff];
322-
const guess = parseInt(guessInput.value);
323-
324-
if (isNaN(guess) || guess < config.min || guess > config.max) {
325-
feedback.textContent = `⚠️ Please enter a number between ${config.min} and ${config.max}!`;
326-
feedback.style.color = 'var(--warning-color)';
327-
return;
328-
}
323+
const config = DIFFICULTIES[currentDiff];
324+
const guess = parseInt(guessInput.value);
325+
326+
// Simple validation - just check if it's a number
327+
if (isNaN(guess)) {
328+
feedback.textContent = `⚠️ Please enter a valid number!`;
329+
feedback.style.color = 'var(--warning-color)';
330+
guessInput.value = '';
331+
guessInput.focus();
332+
return;
333+
}
334+
335+
// Check if guess is within current possible range
336+
if (guess < minRange || guess > maxRange) {
337+
feedback.textContent = `⚠️ Please enter a number between ${minRange} and ${maxRange}!`;
338+
feedback.style.color = 'var(--warning-color)';
339+
guessInput.value = '';
340+
guessInput.focus();
341+
return;
342+
}
329343

330-
attempts++;
331-
const remaining = maxAttempts - attempts;
332-
attemptsLeftEl.textContent = remaining;
333-
updateBar(remaining, maxAttempts);
334-
335-
if (guess === secretNumber) {
336-
feedback.textContent = `🎉 Correct! You found it in ${attempts} attempt${attempts === 1 ? '' : 's'}!`;
337-
feedback.style.color = 'var(--success-color)';
338-
guessInput.disabled = true;
339-
submitBtn.disabled = true;
340-
saveBestScore(currentDiff, attempts);
344+
attempts++;
345+
const remaining = maxAttempts - attempts;
346+
attemptsLeftEl.textContent = remaining;
347+
updateBar(remaining, maxAttempts);
348+
349+
if (guess === secretNumber) {
350+
feedback.textContent = `🎉 Correct! You found it in ${attempts} attempt${attempts === 1 ? '' : 's'}!`;
351+
feedback.style.color = 'var(--success-color)';
352+
guessInput.disabled = true;
353+
submitBtn.disabled = true;
354+
saveBestScore(currentDiff, attempts);
355+
} else {
356+
if (guess < secretNumber) {
357+
feedback.textContent = '📈 Too low! Try higher!';
358+
minRange = guess + 1;
341359
} else {
342-
if (guess < secretNumber) {
343-
feedback.textContent = '📈 Too low! Try higher!';
344-
feedback.style.color = 'var(--primary-color)';
345-
minRange = Math.max(minRange, guess + 1);
360+
feedback.textContent = '📉 Too high! Try lower!';
361+
maxRange = guess - 1;
362+
}
363+
364+
feedback.style.color = guess < secretNumber ? 'var(--primary-color)' : 'var(--danger-color)';
365+
366+
// Update the input's min/max attributes
367+
guessInput.min = minRange;
368+
guessInput.max = maxRange;
369+
370+
// Display narrowed range
371+
rangeDisplay.textContent = `${minRange}${maxRange}`;
372+
373+
if (remaining <= 1) {
374+
if (remaining === 1) {
375+
feedback.textContent = `⚠️ Last attempt! The number is between ${minRange} and ${maxRange}`;
346376
} else {
347-
feedback.textContent = '📉 Too high! Try lower!';
348-
feedback.style.color = 'var(--danger-color)';
349-
maxRange = Math.min(maxRange, guess - 1);
350-
}
351-
rangeDisplay.textContent = `${minRange}${maxRange}`;
352-
353-
if (remaining <= 0) {
354377
feedback.textContent = `💀 Out of attempts! The number was ${secretNumber}.`;
355378
feedback.style.color = 'var(--danger-color)';
356-
guessInput.disabled = true;
357-
submitBtn.disabled = true;
379+
guessInput.disabled = true;
380+
submitBtn.disabled = true;
358381
}
359382
}
360-
361-
guessInput.value = '';
362383
}
363384

385+
guessInput.value = '';
386+
guessInput.focus();
387+
}
388+
364389
// Progress bar
365390
function updateBar(left, total) {
366391
const pct = (left / total) * 100;

web-app/js/projects/pascal-triangle.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ function initPascalTriangle() {
389389
const legendContainer = document.getElementById('pascalLegend');
390390
if (!legendContainer) return;
391391

392-
legendContainer.textContent = (legends[mode] || []).map(([c, l]) =>
392+
legendContainer.innerHTML = (legends[mode] || []).map(([c, l]) =>
393393
`<span style="display:flex;align-items:center;gap:6px;font-size:0.8rem;">
394394
<span style="width:12px;height:12px;border-radius:3px;background:${c};display:inline-block;flex-shrink:0;border:1px solid rgba(255,255,255,0.1)"></span>${l}
395395
</span>`
@@ -527,14 +527,14 @@ function initPascalTriangle() {
527527
}
528528
const cellFontSize = Math.max(8, Math.floor(hexSize * fontSizePercent));
529529

530-
hexagon.textContent = `
530+
hexagon.innerHTML = `
531531
<div class="hexagon-inner" style="--hex-color:${color}; font-size:${cellFontSize}px" title="Value: ${numStr}">
532532
${numStr}
533533
</div>
534534
`;
535535
} else {
536536
// Render colored tiles only, exact values visible on native hover tooltips
537-
hexagon.textContent = `
537+
hexagon.innerHTML = `
538538
<div class="hexagon-inner" style="--hex-color:${color};" title="Value: ${numStr}"></div>
539539
`;
540540
}

0 commit comments

Comments
 (0)