COMMON ERRORS

; (<operator> <argument-1> <argument-2> ...)
; PROBLEM: each line of code contains errors try and fix em
; comment out line if you don't know how
; use the cheatsheet:
; https://howtocode.pages.dev/cheatsheet.html

(if (or (or #true #false) (and #true #false) (+ 1 1)) (+ 4 2))

(define v ("name")
(cond [((string=? v "name") #true)])

((+ 4 4))

(string-append "I am " 21 " years old")

(+ 1 5 - 3 9)

(define fx (+ 1 2 "1"))

(fx)

(define (g a)
  (+ a 1) (g 5))

Answer
; (<operator> <argument-1> <argument-2> ...)
; PROBLEM: each line of code contains errors try and fix em
; comment out line if you don't know how
; use the cheatsheet:
; https://howtocode.pages.dev/cheatsheet.html

; missing parts, we can only presume that
; (+ 1 1) needs to be outside the or
(if (or (or #true #false) (and #true #false)) (+ 1 1) (+ 4 2))

; too many parenthesis
; missing closing parens 
(define v "name")
; too many parentheses
(cond [(string=? v "name") #true])

; too many parens, 8 is not an operation
(+ 4 4)

; string-append cannot take a number
; so you have to convert it
(string-append "I am " (number->string 21) " years old")

; to little parens, + only takes numbers
(+ 1 5 (- 3 9))

(define fx (+ 1 2 1))

; this is not a function
fx 

; someone accidently wrapped the function call
(define (g a)
  (+ a 1))

(g 5)

#Syntax errors

Syntax errors are errors that prevent code from even running until you fix missing parentheses, spelling, etc. These are usually very easy to spot and you get a friendly error message giving you a hint on how to fix them.

A common mistake that beginners do is to randomly type more code, and more parentheses to brute force it to work, but this will only make things worse. Take a deep breath and be very clear on what you need to do and what the code is doing.

Usually syntax errors are things you just learn from trial and error, and someone constantly correcting you so feel free to ask on the racket discord or comments below.

#Logic errors

These errors are silent meaning they can be hard to find since they don’t crash, but the program is misbehaving. Use the stepper!

Problem 1

; PROBLEM BUGGY 1: 
; there is a bug in this program, get all the tests to pass

(check-expect (is-dead? 4) #false)
(check-expect (is-dead? 0) #true)
(check-expect (is-dead? -1) #true)

; ONLY CHANGE CODE BELOW THIS LINE

(define (is-dead? hp)
    (= hp 0))

Answer
; PROBLEM BUGGY 1: 
; there is a bug in this program, get all the tests to pass

(check-expect (is-dead? 4) #false)
(check-expect (is-dead? 0) #true)
(check-expect (is-dead? -1) #true)

; ONLY CHANGE CODE BELOW THIS LINE

(define (is-dead? hp)
    (<= hp 0))

Problem 2

; PROBLEM BUGGY 2
; there is a bug in this program, a user is trying to login:

(check-expect (login "Bob123") "sucessful login")
(check-expect (login "bob123") "sucessful login")
(check-expect (login "alice137") "user not found")

; ONLY CHANGE CODE BELOW THIS LINE

(define (login user)
    (if (string=? user "bob123")
        "sucessful login"
        "user not found"))

Answer
; PROBLEM BUGGY 2
; there is a bug in this program, a user is trying to login:

(check-expect (login "Bob123") "sucessful login")
(check-expect (login "bob123") "sucessful login")
(check-expect (login "alice137") "user not found")

; ONLY CHANGE CODE BELOW THIS LINE

(define (login user)
    (if (string=? (string-lowercase user) "bob123")
        "sucessful login"
        "user not found"))

Problem 3

; PROBLEM BUGGY 3: THERE IS A BUG IN THIS CODE
(check-expect (area 4 4) 16)
(check-expect (area 2 4) 8)

; ONLY CHANGE CODE BELOW THIS LINE

(define (area w h)
    (* w w))

Answer
; PROBLEM BUGGY 3: THERE IS A BUG IN THIS CODE
(check-expect (area 4 4) 16)
(check-expect (area 2 4) 8)

; ONLY CHANGE CODE BELOW THIS LINE

(define (area w h)
    (* w h))

Problem 4

; PROBLEM BUGGY 4: THERE IS A BUG IN THIS CODE:
(check-expect (18-or-older? 4) #false)
(check-expect (18-or-older? 17) #false)
(check-expect (18-or-older? 18) #true)
(check-expect (18-or-older? 19) #true)
(check-expect (18-or-older? 22) #true)

; ONLY CHANGE CODE BELOW THIS LINE

(define (18-or-older? age)
    (> age 18))

Answer
; PROBLEM BUGGY 4: THERE IS A BUG IN THIS CODE:
(check-expect (18-or-older? 4) #false)
(check-expect (18-or-older? 17) #false)
(check-expect (18-or-older? 18) #true)
(check-expect (18-or-older? 19) #true)
(check-expect (18-or-older? 22) #true)

; ONLY CHANGE CODE BELOW THIS LINE

(define (18-or-older? age)
    (>= age 18))

#Videos

Common Erorrs