☰ Menu

Home › Blog

How to Code a FLAMES Calculator in Python and JavaScript

By the FlamesCalculator.net team · December 5, 2026 · 8 min read

flames.pydef flames(a, b):n = count_left(a, b)return result(n)</>

A FLAMES calculator is a friendly first programming project. The whole game fits in a few dozen lines, you can check every answer by hand, and it touches skills you will use again and again: cleaning text, counting, looping, working with lists and handling unusual input. This tutorial builds it twice, first in Python and then in JavaScript, and explains why each line is there. You only need to know what a variable, a loop and a function are.

If you have never played the game, read the beginner's guide first or try a pair of names in the calculator. It helps to know what a correct answer looks like before you ask a computer to produce one.

Start by writing the rules in plain English

Good programs begin with a clear description. If you cannot explain the steps to a friend, you will struggle to explain them to a computer. Here is the version we will code.

  1. Take two names.
  2. Convert them to lowercase and keep only letters.
  3. Cancel matching letters one for one: each letter in the first name can remove at most one letter in the second.
  4. Add up the letters that remain in both names. Call that number n.
  5. Make a list of the six letters F, L, A, M, E and S.
  6. Count n places around the list, remove the letter you land on, and continue counting from the next letter.
  7. Stop when one letter is left.

Step seven hides a question that every programmer should ask: what happens when something unexpected occurs? If both names use exactly the same letters, n is zero and there is nothing to count. We will treat that as "no result" instead of inventing an answer.

Step 1: Clean the input

People type names in all sorts of ways. "Mary Kate", "mary-kate" and "MARYKATE" should be treated alike. In Python, the string method isalpha tells you whether a character is a letter, so a short function can do the cleaning.

def clean(name):
    return [ch for ch in name.lower() if ch.isalpha()]

The function returns a list of characters instead of a string because lists are easy to remove items from. Calling clean("Mary-Kate") gives the eight letters m, a, r, y, k, a, t, e. The hyphen disappears. Note that isalpha also accepts accented letters such as é. That means "Zoé" and "Zoe" are not identical to this program. You could decide to strip accents, but decide on purpose and tell your users which rule you chose.

Step 2: Cancel the shared letters

This is where most first attempts go wrong. It is tempting to remove every letter that appears in both names, but that is not the rule. If one name has two m's and the other has one, only one pair cancels. The simplest correct approach is to walk through the first name and, for each letter, try to remove one copy from the second.

def count_left(first, second):
    a = clean(first)
    b = clean(second)
    left_in_a = 0
    for ch in a:
        if ch in b:
            b.remove(ch)      # removes only one copy
        else:
            left_in_a += 1
    return left_in_a + len(b)

The important detail is that list.remove deletes only the first match. Once a letter in the second name has been used, it is gone and cannot be matched again. Letters from the first name that find no partner are counted, and whatever survives in the second list is added at the end.

Try it with Olivia and Noah. The o and the a are shared. Olivia keeps l, i, v and i, while Noah keeps n and h. The function returns 6.

A shorter way with Counter

Python's collections module has a Counter type that counts how many times each item appears. Subtracting one Counter from another keeps only the positive leftovers, which is exactly what cancellation means.

from collections import Counter

def count_left(first, second):
    a = Counter(clean(first))
    b = Counter(clean(second))
    return sum((a - b).values()) + sum((b - a).values())

Both versions give the same number. The first is better for learning because you can see each match happen. The second is tidier once you understand the idea.

Step 3: The elimination loop

Now we count around a shrinking list. Programmers number list positions from zero, so F is position 0 and S is position 5. If we start at position i and count n letters, the one we land on is at position i + n - 1. The minus one is there because the starting letter counts as the first. The percent sign gives the remainder after division, which wraps the position back to the beginning of the list.

def eliminate(n):
    letters = list("FLAMES")
    removed = []
    i = 0
    while len(letters) > 1:
        i = (i + n - 1) % len(letters)
        removed.append(letters.pop(i))
    return letters[0], removed

After pop removes a letter, every later letter shifts back by one place, so position i already points to the next letter. There is no need to add anything. If the removed letter was last in the list, i equals the new length, and the remainder in the next round brings it back to the front.

For n equal to 6, the letters leave in the order S, F, A, L, E, and M remains. That is Marriage. The article on the math behind FLAMES explains why this loop is a version of a classic counting puzzle.

Step 4: Put it together

RESULTS = {"F": "Friends", "L": "Lovers", "A": "Affection",
           "M": "Marriage", "E": "Enemies", "S": "Siblings"}

def flames(first, second):
    if not clean(first) or not clean(second):
        return "Please enter two names."
    n = count_left(first, second)
    if n == 0:
        return "All letters cancel, so there is no result."
    letter, _ = eliminate(n)
    return RESULTS[letter]

print(flames("Olivia", "Noah"))    # Marriage
print(flames("Emma", "Liam"))      # Enemies
print(flames("Emily", "Jacob"))    # Lovers

Notice that the checks come first. A function that rejects bad input early is easier to read than one that discovers a problem halfway through.

The same program in JavaScript

JavaScript runs in every web browser, which makes it the natural choice if you want a page your friends can open. The logic does not change, only the spelling of it.

function clean(name) {
  return Array.from(name.toLowerCase()).filter(ch => /\p{L}/u.test(ch));
}

function countLeft(first, second) {
  const b = clean(second);
  let leftInA = 0;
  for (const ch of clean(first)) {
    const at = b.indexOf(ch);
    if (at > -1) b.splice(at, 1);
    else leftInA++;
  }
  return leftInA + b.length;
}

function eliminate(n) {
  const letters = Array.from("FLAMES");
  let i = 0;
  while (letters.length > 1) {
    i = (i + n - 1) % letters.length;
    letters.splice(i, 1);
  }
  return letters[0];
}

Two things deserve attention. First, splice changes the array itself, much like pop and remove in Python. Second, call eliminate only when n is greater than zero. With zero, the position arithmetic can produce a negative number and the answer would be meaningless.

Add a simple web page

<input id="a" placeholder="First name">
<input id="b" placeholder="Second name">
<button id="go">Calculate</button>
<p id="out"></p>

<script>
  // paste the three functions here
  const names = {F:"Friends", L:"Lovers", A:"Affection",
                 M:"Marriage", E:"Enemies", S:"Siblings"};
  document.getElementById("go").onclick = () => {
    const a = document.getElementById("a").value;
    const b = document.getElementById("b").value;
    const n = countLeft(a, b);
    const ok = clean(a).length && clean(b).length && n > 0;
    document.getElementById("out").textContent =
      ok ? names[eliminate(n)] : "No result. Check the names.";
  };
</script>

Save this as a file ending in .html and open it in a browser. Use textContent, not innerHTML, whenever you display something a person typed. textContent shows the characters as text, while innerHTML would treat them as page code, which can open a security hole.

Test your calculator

Do not assume the program works because it printed something. Compare it with answers worked out another way. This table makes a handy test set.

NamesLetters leftExpected
Olivia and Noah6Marriage
Emma and Liam4Enemies
Chloe and Mason8Affection
Harper and Logan9Enemies
Emily and Jacob10Lovers
Mia and Liam1Siblings

Then try the awkward cases: an empty box, a name made only of numbers, two identical names, very long names, and names with spaces or apostrophes. In Python you can turn the table into automatic checks with assert statements, so that a later change cannot quietly break an earlier answer.

Common bugs and how to spot them

Ideas to extend the project

Once the basic version works, you have a small laboratory for learning more.

What this project teaches

You practiced breaking a game into steps, cleaning input, handling duplicates, using the remainder operator and testing against known answers. Those habits matter far more than the game itself. The same patterns appear in word puzzles, scoring systems and schedulers that rotate through a list of people.

Frequently asked questions

Which language should a beginner choose?

Choose Python if you want to focus on logic with little setup. Choose JavaScript if you want something that runs as a web page.

Do I need a server or database?

No. The whole calculation can run on the user's own device, which also means you do not have to collect anyone's names.

Can the code predict compatibility?

No. It follows a counting rule. It is a fun exercise and says nothing about real people.

Try the FLAMES calculator

Keep reading