@@ -74,8 +74,13 @@ function init() {
7474 // Load challenge from URL parameter or default to 0
7575 const urlParams = new URLSearchParams ( window . location . search ) ;
7676 const challengeParam = urlParams . get ( "challenge" ) ;
77+ // Support both numeric and string challenge IDs (e.g., "debug")
7778 const initialChallenge =
78- challengeParam !== null ? parseInt ( challengeParam ) : 0 ;
79+ challengeParam !== null
80+ ? isNaN ( parseInt ( challengeParam ) )
81+ ? challengeParam
82+ : parseInt ( challengeParam )
83+ : 0 ;
7984 loadChallenge ( initialChallenge ) ;
8085
8186 // Hide loading overlay
@@ -265,7 +270,11 @@ function setupEventListeners() {
265270 stopExecution ( ) ;
266271 }
267272 App . hasRun = false ;
268- const challengeId = parseInt ( e . currentTarget . dataset . challenge ) ;
273+ const rawChallenge = e . currentTarget . dataset . challenge ;
274+ // Support both numeric and string challenge IDs
275+ const challengeId = isNaN ( parseInt ( rawChallenge ) )
276+ ? rawChallenge
277+ : parseInt ( rawChallenge ) ;
269278
270279 // Update URL to reflect the new challenge
271280 const newUrl = `simulator.html?challenge=${ challengeId } ` ;
@@ -297,7 +306,13 @@ function setupEventListeners() {
297306 window . addEventListener ( "popstate" , ( e ) => {
298307 const urlParams = new URLSearchParams ( window . location . search ) ;
299308 const challengeParam = urlParams . get ( "challenge" ) ;
300- const challengeId = challengeParam !== null ? parseInt ( challengeParam ) : 0 ;
309+ // Support both numeric and string challenge IDs
310+ const challengeId =
311+ challengeParam !== null
312+ ? isNaN ( parseInt ( challengeParam ) )
313+ ? challengeParam
314+ : parseInt ( challengeParam )
315+ : 0 ;
301316
302317 if ( App . isRunning ) {
303318 stopExecution ( ) ;
@@ -413,11 +428,13 @@ function loadChallenge(challengeId) {
413428 // Update dropdown text
414429 const dropdownItems = document . querySelectorAll ( "[data-challenge]" ) ;
415430 dropdownItems . forEach ( ( item ) => {
416- item . classList . toggle (
417- "active" ,
418- parseInt ( item . dataset . challenge ) === challengeId
419- ) ;
420- if ( parseInt ( item . dataset . challenge ) === challengeId ) {
431+ // Compare as strings to support both numeric and string IDs
432+ const itemChallenge = item . dataset . challenge ;
433+ const isActive =
434+ itemChallenge === String ( challengeId ) ||
435+ parseInt ( itemChallenge ) === challengeId ;
436+ item . classList . toggle ( "active" , isActive ) ;
437+ if ( isActive ) {
421438 App . elements . challengeDropdown . innerHTML = item . innerHTML ;
422439 }
423440 } ) ;
@@ -1834,6 +1851,57 @@ function hideLoading() {
18341851 */
18351852function getStarterCodes ( ) {
18361853 return {
1854+ debug : `# Debug Script: Hardware Sanity Test
1855+ # Runs a sequence of movements and distance readings
1856+ # This mirrors project/main.py for testing on real hardware
1857+
1858+ from aidriver import AIDriver, hold_state
1859+ import aidriver
1860+
1861+ aidriver.DEBUG_AIDRIVER = True
1862+
1863+ print("Initialising AIDriver hardware test...")
1864+ my_robot = AIDriver()
1865+
1866+ print("Starting tests in 3 seconds. Ensure clear space around the robot.")
1867+ hold_state(3)
1868+
1869+ # Test 1: Drive Forward
1870+ print("Test 1: drive_forward")
1871+ my_robot.drive_forward(200, 200)
1872+ hold_state(2)
1873+ my_robot.brake()
1874+ hold_state(1)
1875+
1876+ # Test 2: Drive Backward
1877+ print("Test 2: drive_backward")
1878+ my_robot.drive_backward(200, 200)
1879+ hold_state(2)
1880+ my_robot.brake()
1881+ hold_state(1)
1882+
1883+ # Test 3: Rotate Right
1884+ print("Test 3: rotate_right")
1885+ my_robot.rotate_right(200)
1886+ hold_state(2)
1887+ my_robot.brake()
1888+ hold_state(1)
1889+
1890+ # Test 4: Rotate Left
1891+ print("Test 4: rotate_left")
1892+ my_robot.rotate_left(200)
1893+ hold_state(2)
1894+ my_robot.brake()
1895+ hold_state(1)
1896+
1897+ # Test 5: Ultrasonic Sensor
1898+ print("Test 5: ultrasonic distance readings")
1899+ for i in range(5):
1900+ distance = my_robot.read_distance()
1901+ hold_state(0.5)
1902+
1903+ print("All hardware tests completed.")
1904+ ` ,
18371905 0 : `# Challenge 0: Fix the Code
18381906# This code has errors - can you find and fix them?
18391907
0 commit comments