How to Code a FLAMES Calculator in Python and JavaScript
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.
- Take two names.
- Convert them to lowercase and keep only letters.
- Cancel matching letters one for one: each letter in the first name can remove at most one letter in the second.
- Add up the letters that remain in both names. Call that number n.
- Make a list of the six letters F, L, A, M, E and S.
- Count n places around the list, remove the letter you land on, and continue counting from the next letter.
- 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.
| Names | Letters left | Expected |
|---|---|---|
| Olivia and Noah | 6 | Marriage |
| Emma and Liam | 4 | Enemies |
| Chloe and Mason | 8 | Affection |
| Harper and Logan | 9 | Enemies |
| Emily and Jacob | 10 | Lovers |
| Mia and Liam | 1 | Siblings |
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
- Removing all copies of a letter. If Emma and Liam give a count other than 4, check your handling of the double m.
- Forgetting the minus one. Without it, every answer shifts by one position.
- Restarting at F after each removal. Counting must continue from the next letter.
- Ignoring the zero case. Identical names should produce a message, not a crash or a random word.
- Comparing capital and small letters. Convert to one case before matching.
Ideas to extend the project
Once the basic version works, you have a small laboratory for learning more.
- Show each step: which letters were canceled and the order in which F-L-A-M-E-S letters were removed.
- Add a loop in Python that tests every pair in a list of fictional characters and counts how often each result appears.
- Let users change the word, for example to a six-letter word of their own, and give each letter a meaning.
- Improve accessibility by adding labels to the inputs and announcing the result to screen readers with an aria-live attribute.
- Add a share button, but remember that a link containing names reveals those names to anyone who receives it. Our privacy guide explains what to watch for.
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 calculatorKeep reading
FLAMES in the Classroom: A Math and Logic Activity for Students
A teacher-friendly lesson that uses the FLAMES letter game to practice remainders, data collection, probability and clear reasoning.
9 Paper Games Like FLAMES for Road Trips and Rainy Days
MASH, dots and boxes, hangman and more: nine easy pen-and-paper games to play after FLAMES, with rules, tips and kid-friendly twists.
Crush Games and Real Feelings: What a FLAMES Result Cannot Tell You
Got a result that excited or upset you? A calm guide for teens on crush games, real feelings, honest conversations and respecting a no.