Spot the bug - past puzzles

Past puzzles

2025-05-06

Submitted at

2023-11-29 02:27:40
css
body {
    background-color: #GGG;
}

Solution

The color code '#GGG' is not a valid hexadecimal color representation. It should be replaced with a valid hex code like '#FFF'.

2025-05-05

Submitted at

2023-11-29 02:27:40
javascript
function greet(name) {
    return 'Hello, ' + name;
}
greet();

Solution

The function greet is called without arguments, which will result in 'Hello, undefined'. A default value or a check should be implemented.

2025-05-04

Submitted at

2023-11-29 02:27:40
python
def divide_numbers(a, b):
    return a / b

Solution

The function does not handle division by zero, which will cause an exception. A check should be added to handle this scenario.

2025-04-26

Submitted at

2025-04-22 10:15:55
css
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 100;
}

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  padding: 20px;
  z-index: 10;
}

Solution

The bug is that the modal's z-index (10) is lower than the backdrop's z-index (100), so the modal will be hidden behind the backdrop. The modal's z-index should be higher than the backdrop's.

2025-04-25

Submitted at

2025-04-15 14:40:18
bash
#!/bin/bash

filename=$1

if [ -f $filename ]; then
  echo "Processing $filename"
  grep "ERROR" $filename > errors.log
else
  echo "File not found"
fi

Solution

The bug is missing quotes around the $filename variable. If the filename contains spaces, the script will fail. It should be: if [ -f "$filename" ] and grep "ERROR" "$filename" > errors.log

2025-04-24

Submitted at

2025-04-12 11:20:33
csharp
public class UserManager {
  private List<User> users;
  
  public void AddUser(User user) {
    users.Add(user);
  }
  
  public User FindUser(string username) {
    return users.FirstOrDefault(u => u.Username == username);
  }
}

Solution

The bug is that the 'users' list is never initialized. In the constructor, it should include 'users = new List();' Otherwise, when AddUser is called, it will throw a NullReferenceException.

2025-04-23

Submitted at

2025-04-09 14:22:33
javascript
function createCounter() {
  let count = 0;
  
  function increment() {
    count++;
    return count;
  }
  
  return {
    increment: increment,
    reset: function() {
      count = 0;
    },
    getCount: function() {
      return Count;
    }
  };
}

Solution

The bug is in the getCount method. It tries to return 'Count' (with capital C) instead of 'count' (lowercase). JavaScript is case-sensitive, so this will cause a reference error.

2025-04-22

Submitted at

2025-04-17 14:23:10
typescript
function reverseString(str: string): string {
  let reversed = '';
  for (let i = 0; i <= str.length; i++) {
    reversed = str[i] + reversed;
  }
  return reversed;
}

Solution

The loop condition uses <= instead of <, causing it to access an undefined character at str.length. Also, when i=str.length, str[i] is undefined and will appear as 'undefined' at the beginning of the result.

2025-04-21

Submitted at

2025-04-17 14:24:55
javascript
async function fetchUserData(userId) {
    const response = await fetch(`/api/users/${userId}`);
    const data = response.json();
    return data;
}

Solution

The function doesn't await the response.json() promise, which means it returns a Promise instead of the resolved data.

2025-04-20

Submitted at

2025-04-17 14:28:00
csharp
public class User
{
    private string _name;
    private int _age;
    
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    
    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }
    
    public bool IsAdult()
    {
        return Age > 18;
    }
}

Solution

The IsAdult method checks if Age > 18, but the legal adult age is typically 18 and above, so it should be Age >= 18 instead.

2025-04-19

Submitted at

2025-04-17 14:36:40
typescript
interface User {
  id: number;
  name: string;
  email: string;
  active: boolean;
}

function filterActiveUsers(users: User[]): User[] {
  return users.filter(user => user.active = true);
}

Solution

The filter callback uses the assignment operator (=) instead of the equality operator (==) or strict equality operator (===). This assigns true to user.active instead of checking if it's equal to true, effectively making all users active. It should be: users.filter(user => user.active === true) or simply users.filter(user => user.active).

2025-04-18

Submitted at

2025-04-12 13:22:06
php
function incrementCounter() {
    static $counter = 0;
    counter++;
    return $counter;
}

echo incrementCounter();

Solution

The variable "counter" is missing a dollar sign

2025-04-17

Submitted at

2025-04-12 13:25:22
javascript
function setupButton() {
    const button = document.getElementById('myButton');
    
    button.addEventListener('click', function() {
        button.textContent = 'Clicked!';
        setupButton();
    });
}

Solution

Remove the recursive call (setupButton) to prevent adding multiple listeners

2025-04-16

Submitted at

2025-04-12 13:21:13
javascript
function getLastElement(array) {
    return array[array.length];
}

const arr = [1, 2, 3, 4, 5];
console.log(getLastElement(arr));

Solution

Change "array[array.length]" to "array[array.length - 1]". The last element's index is always one less than the array's length.

2025-04-15

Submitted at

2025-04-12 13:18:02
css
.container {
    width: 100;
    height: 200px;
    padding: 20px;
    margin: 10px;
}

Solution

100 what? cows? No pixels: 100px;

2025-04-14

Submitted at

2025-04-12 13:16:22
bash
#!/bin/bash
if [ $1 -eq "yes" ]; then
  echo "You said yes"
fi

Solution

Use = to compare strings.

2025-04-13

Submitted at

2025-04-08 12:12:18
php
function sanitizeInput($input)
{
    $clean = strip_tags($input);
    $clean = htmlspecialchars($clean);
    return $input;
}

Solution

The bug is that the function returns the original $input instead of the sanitized $clean variable, making the sanitization ineffective.