from js import document, localStorage, window
import random, re, sys, io
START = '''def greet_and_add(name, a, b):
# 1) say Hello, NAME! using name.upper()
# 2) print the sum of a and b
pass
'''
SOLN = '''def greet_and_add(name, a, b):
print("Hello, " + name.upper() + "!")
print(a + b)
'''
names = ["Kai","Ava","Milo","Zoe","Lia","Noah","Ivy","Owen","Maya","Leo"]
NAME = random.choice(names); A = 2; B = 3
document.getElementById('nm').textContent = NAME
document.getElementById('aa').textContent = str(A)
document.getElementById('bb').textContent = str(B)
def gi(k):
v = localStorage.getItem(k)
try: return int(v) if v is not None else 0
except: return 0
def setb(stars=None, bugs=None):
if stars is not None: localStorage.setItem('stars', str(stars))
if bugs is not None: localStorage.setItem('bugs', str(bugs))
document.getElementById('stars').textContent = f'⭐ Stars: {gi("stars")}'
document.getElementById('bugs').textContent = f'🐞 Bugs: {gi("bugs")}'
def run(evt):
ns = {}; src = document.getElementById('editor').value; out_el = document.getElementById('out')
f = io.StringIO()
try:
exec(src, ns)
assert 'greet_and_add' in ns, "Make a function named greet_and_add(name, a, b)."
old = sys.stdout; sys.stdout = f
try: ns['greet_and_add'](NAME, A, B)
finally: sys.stdout = old
output = f.getvalue()
simple = re.sub(r'[^A-Z0-9 !+-]', '', output.upper())
assert ("HELLO " + NAME.upper() + "!") in simple, "Say 'Hello, NAME!' (uppercase)."
assert str(A+B) in simple, "Print the sum of a and b."
out_el.textContent = "All checks passed! ⭐\n" + output
setb(stars=gi('stars')+1)
try: window.confetti()
except: pass
except Exception as e:
out_el.textContent = "Bug found: " + str(e)
setb(bugs=gi('bugs')+1)
document.getElementById('run').addEventListener('click', run)
document.getElementById('fill-start').addEventListener('click',
lambda e: setattr(document.getElementById('editor'), 'value', START))
document.getElementById('fill-solution').addEventListener('click',
lambda e: setattr(document.getElementById('editor'), 'value', SOLN))
if not document.getElementById('editor').value:
document.getElementById('editor').value = START
setb()