Skip to content

Commit 47fb78d

Browse files
Fix console expected output comparison logic
- Use normalizedQuestion.expectedOutput as primary source for expected output - Fallback to currentTopicQuestion.expectedOutput if normalized version unavailable - Ensure both success and error cases use consistent expected output source - Fix setTimeout-based questions showing incorrect expected output - Improve console comparison accuracy for var vs let scoping questions
1 parent 4eed9b9 commit 47fb78d

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

src/App.jsx

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,11 @@ function App() {
459459
setResult(null)
460460
}
461461

462-
const runCode = () => {
462+
const runCode = async () => {
463463
try {
464464
const consoleOutput = []
465+
const pendingTimeouts = []
466+
465467
const mockConsole = {
466468
log: (...args) => {
467469
consoleOutput.push(args.map(arg =>
@@ -470,16 +472,42 @@ function App() {
470472
}
471473
}
472474

473-
const func = new Function('console', userCode)
474-
func(mockConsole)
475+
// Mock setTimeout to handle async operations properly
476+
const mockSetTimeout = (callback, delay) => {
477+
const promise = new Promise(resolve => {
478+
setTimeout(() => {
479+
callback()
480+
resolve()
481+
}, delay)
482+
})
483+
pendingTimeouts.push(promise)
484+
return pendingTimeouts.length // Return a fake timeout ID
485+
}
486+
487+
const func = new Function('console', 'setTimeout', userCode)
488+
func(mockConsole, mockSetTimeout)
475489

476-
const output = consoleOutput.join('\n')
477-
const isCorrect = output === currentTopicQuestion.expectedOutput
490+
// Wait for all setTimeout callbacks to complete
491+
await Promise.all(pendingTimeouts)
478492

493+
// Join output - check if it should be comma-separated or newline-separated
494+
const output = consoleOutput.length > 0 ? consoleOutput.join(', ') : ''
495+
496+
// Also try newline format in case that's expected
497+
const outputNewline = consoleOutput.length > 0 ? consoleOutput.join('\n') : ''
498+
499+
// Use normalized question's expected output if available, fallback to currentTopicQuestion
500+
const expectedOutput = normalizedQuestion?.expectedOutput || currentTopicQuestion.expectedOutput
501+
502+
// Check both formats against expected output
503+
const isCorrectComma = output === expectedOutput
504+
const isCorrectNewline = outputNewline === expectedOutput
505+
const isCorrect = isCorrectComma || isCorrectNewline
506+
479507
setResult({
480-
output,
508+
output: output || '(no output)',
481509
isCorrect,
482-
expected: currentTopicQuestion.expectedOutput
510+
expected: expectedOutput
483511
})
484512

485513
if (isCorrect) {
@@ -509,12 +537,14 @@ function App() {
509537
normalizedError = 'SyntaxError'
510538
}
511539

512-
const isCorrect = normalizedError === currentTopicQuestion.expectedOutput
540+
// Use normalized question's expected output if available, fallback to currentTopicQuestion
541+
const expectedOutput = normalizedQuestion?.expectedOutput || currentTopicQuestion.expectedOutput
542+
const isCorrect = normalizedError === expectedOutput
513543

514544
setResult({
515545
output: errorOutput,
516546
isCorrect,
517-
expected: currentTopicQuestion.expectedOutput
547+
expected: expectedOutput
518548
})
519549

520550
if (isCorrect) {

0 commit comments

Comments
 (0)