How Do You Write a FLAMES Program in Java and JavaScript (With Code and Logic)?
Learn how to write a FLAMES program in Java and JavaScript with complete source code, algorithm logic, worked examples, edge case handling, time complexity analysis, and ideas for turning the code into a web calculator.
A FLAMES program is a string-manipulation coding exercise that takes 2 names as input, cancels common letters between them, counts remaining characters, and eliminates letters from the word F-L-A-M-E-S in a circular pattern until 1 letter remains. The acronym stands for Friends (F), Lovers (L), Affection (A), Marriage (M), Enemies (E), and Siblings (S). Writing a FLAMES program is a popular beginner project for practicing string operations, loops, arrays, and list manipulation. This post covers the algorithm logic with pseudocode, complete working Java and JavaScript programs, edge case handling, time and space complexity, and ideas for turning the program into a web calculator.
What Is a FLAMES Program, and What Should It Do?
A FLAMES program takes 2 names as input, removes common letters, counts the remaining letters, and uses that count to eliminate letters from the string “FLAMES” until 1 letter remains. The final letter maps to 1 of 6 relationship labels: Friends (F), Lovers (L), Affection (A), Marriage (M), Enemies (E), or Siblings (S).
The expected input is 2 name strings. The expected output is 1 relationship label. The FLAMES program is a common beginner exercise in Java and JavaScript programming courses for practicing 4 programming concepts:
- String manipulation: Converting to lowercase, removing spaces, handling character arrays
- Loop structures: For loops, while loops, and circular counting
- List/array operations: Removing elements at specific indices
- Modular arithmetic: Using the modulo operator to cycle through a list
What Is the Logic Behind the FLAMES Algorithm?
The algorithm logic exactly mirrors the authentic pen-and-paper rules of the game. First, clean both names by removing spaces and converting to lowercase. Second, cancel common letters one-for-one. Third, count the remaining letters to get the elimination count. Finally, count circularly through the array [“F”, “L”, “A”, “M”, “E”, “S”], removing the landed letter at each step until one remains. For example, “ravi” and “priya” share “r”, “a”, and “i”. The 3 remaining letters (“v”, “p”, “y”) mean you eliminate every 3rd letter until only “E” (Enemies) is left.
How Do You Write a FLAMES Program in Java?
To write a FLAMES program in Java, use Scanner for input and ArrayList for the elimination loop. Here is the complete, tested Java source code, followed by the sample input and output:
import java.util.ArrayList;
import java.util.Scanner;
public class FlamesGame {
public static String calculateFlames(String name1, String name2) {
// Step 1: Clean both names
char[] chars1 = name1.toLowerCase().replaceAll("[^a-z]", "").toCharArray();
char[] chars2 = name2.toLowerCase().replaceAll("[^a-z]", "").toCharArray();
// Step 2: Cancel common letters one-for-one
boolean[] used1 = new boolean[chars1.length];
boolean[] used2 = new boolean[chars2.length];
for (int i = 0; i < chars1.length; i++) {
for (int j = 0; j < chars2.length; j++) {
if (!used1[i] && !used2[j] && chars1[i] == chars2[j]) {
used1[i] = true;
used2[j] = true;
break;
}
}
}
// Step 3: Count remaining letters
int count = 0;
for (boolean b : used1) if (!b) count++;
for (boolean b : used2) if (!b) count++;
if (count == 0) return "No result (identical names)";
// Step 4: Initialize FLAMES list
ArrayList<Character> flames = new ArrayList<>();
for (char c : "FLAMES".toCharArray()) flames.add(c);
// Step 5: Eliminate letters circularly
int index = 0;
while (flames.size() > 1) {
index = (index + count - 1) % flames.size();
flames.remove(index);
if (index == flames.size()) index = 0;
}
// Step 6: Map result
char result = flames.get(0);
switch (result) {
case 'F': return "Friends";
case 'L': return "Lovers";
case 'A': return "Affection";
case 'M': return "Marriage";
case 'E': return "Enemies";
case 'S': return "Siblings";
default: return "Unknown";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first name: ");
String name1 = scanner.nextLine();
System.out.print("Enter second name: ");
String name2 = scanner.nextLine();
System.out.println("FLAMES Result: " + calculateFlames(name1, name2));
scanner.close();
}
}
Enter first name: Ravi
Enter second name: Priya
FLAMES Result: Enemies
The Java FLAMES program cleans both names using replaceAll(), cancels common letters using a nested loop with boolean tracking arrays, counts the remaining unused characters, and eliminates FLAMES letters using ArrayList.remove() in a circular pattern.
How Do You Write a FLAMES Program in JavaScript?
To write a FLAMES program in JavaScript, use arrays and the splice() method for the elimination loop. Here is the complete JavaScript FLAMES function that runs in Node.js or a browser console. Once you master this basic letter elimination, you can easily code the math for a percentage score extension as well.
function flames(name1, name2) {
// Step 1: Clean both names
let chars1 = name1.toLowerCase().replace(/[^a-z]/g, "").split("");
let chars2 = name2.toLowerCase().replace(/[^a-z]/g, "").split("");
// Step 2: Cancel common letters one-for-one
for (let i = 0; i < chars1.length; i++) {
let matchIndex = chars2.indexOf(chars1[i]);
if (matchIndex !== -1) {
chars1[i] = null;
chars2[matchIndex] = null;
}
}
// Step 3: Count remaining letters
let remaining1 = chars1.filter(c => c !== null);
let remaining2 = chars2.filter(c => c !== null);
let count = remaining1.length + remaining2.length;
if (count === 0) return "No result (identical names)";
// Step 4: Initialize FLAMES list
let flamesList = ["F", "L", "A", "M", "E", "S"];
// Step 5: Eliminate letters circularly
let index = 0;
while (flamesList.length > 1) {
index = (index + count - 1) % flamesList.length;
flamesList.splice(index, 1);
if (index === flamesList.length) index = 0;
}
// Step 6: Map result
const labels = {
F: "Friends", L: "Lovers", A: "Affection",
M: "Marriage", E: "Enemies", S: "Siblings"
};
return labels[flamesList[0]] || "Unknown";
}
// Sample usage
console.log(flames("Ravi", "Priya")); // Output: Enemies
Enemies
The JavaScript FLAMES function mirrors the Java logic: it cleans names with replace(), cancels common letters using indexOf() with null marking, counts remaining characters, and eliminates FLAMES letters using splice() in a circular pattern. To handle edge cases in either language, you must address case sensitivity, spaces, and repeated letter cancellation.
How Do You Handle Spaces, Uppercase, and Repeated Letters in FLAMES Code?
Case sensitivity and spaces: Convert both names to lowercase and strip all non-alphabetic characters before processing. Both the Java and JavaScript versions above use regex replacements to strip non-letters. For repeated letters, you must use the correct approach (removing 1 match at a time) rather than the buggy approach (removing all matching characters at once).
Buggy approach — removes all matching characters at once:
// WRONG: Removes every 'a' from both names
chars2 = chars2.filter(c => c !== chars1[i]);
// CORRECT: Removes only the first matching 'a'
let matchIndex = chars2.indexOf(chars1[i]);
if (matchIndex !== -1) {
chars1[i] = null;
chars2[matchIndex] = null;
}
The correct approach cancels exactly 1 letter from each name per match. This precise string sanitization and space-removal code ensures that multi-word names or middle names are handled seamlessly. You must also handle the case where both names are identical (0 remaining letters) by returning a default message before entering the elimination loop.
What Are the Time and Space Complexity of the FLAMES Program?
The time complexity of the FLAMES program is O(n × m) for the letter cancellation step, where n and m are the lengths of the 2 cleaned names. The elimination step runs in O(36) at most, which is constant time. Space complexity is O(n + m) for storing the character arrays. For names, performance is never a concern, so prioritize readable code over optimization.
How Do the Java and JavaScript Versions Differ, and Which Should You Use?
The Java and JavaScript FLAMES programs produce identical results using different language features. Use Java for learning core programming concepts and practicing object-oriented programming. Use JavaScript for building a quick web-based FLAMES calculator that runs in any browser. Neither language produces a “better” result — both execute the exact same algorithm.
How Can You Turn Your FLAMES Program Into a Web Calculator or Project?
To turn the JavaScript function into a web calculator, build an HTML page with 2 text input fields and a button that calls the function. To make the project more advanced, try building a group mode array loop to test one name against a list of friends simultaneously. You could also add a step-by-step display, input validation, a result card generator, or a dark mode toggle.
The beauty of building a FLAMES project is that the output logic requires no complex math libraries, backend servers, or external API calls. Everything runs natively in the client’s browser or terminal window.
Once your code is working perfectly, you can deploy the HTML and JavaScript files to any free static hosting service like GitHub Pages or Vercel, allowing anyone to test their names instantly.
The FLAMES game is not a scientific tool, which makes it a low-pressure coding project with no correctness constraints beyond the algorithm itself. Try the free online calculator to see a working implementation, and share your own FLAMES code with the developer community.
Frequently Asked Questions
How do you write a FLAMES program in Java?
To write a FLAMES program in Java, use Scanner for input, clean both names with replaceAll(), cancel common letters using a nested loop with boolean arrays, count remaining characters, and eliminate letters from an ArrayList of “FLAMES” characters using circular indexing.
How do you write a FLAMES program in JavaScript?
To write a FLAMES program in JavaScript, create a function that takes 2 name strings, cleans them with replace(), cancels common letters using indexOf() and null marking, counts remaining characters, and eliminates letters from a “FLAMES” array using splice() and modular arithmetic.
What is the logic of the FLAMES game in programming?
The logic of the FLAMES game in programming follows 6 steps: clean both names, cancel common letters one pair at a time, count remaining letters, initialize the list F-L-A-M-E-S, eliminate letters in a circular pattern using the count, and return the last remaining letter.
How do you remove common characters from two strings in Java?
To remove common characters from 2 strings in Java, use a nested loop that marks matched characters with boolean arrays, cancelling 1 occurrence per match. This approach handles repeated letters correctly by tracking which characters have already been paired.
How do you remove common letters from two strings in JavaScript?
To remove common letters from 2 strings in JavaScript, loop through the first string’s character array and use indexOf() on the second string’s array to find the first unmatched occurrence, then set both matched positions to null.
What is the algorithm for the FLAMES game?
The FLAMES game algorithm is a 6-step process: (1) clean inputs, (2) cancel common letters one-for-one, (3) count remaining letters, (4) initialize the FLAMES letter list, (5) eliminate letters at position (count - 1) % list.length in a circular loop, (6) return the last letter.
How do you eliminate letters in a circular list for FLAMES?
To eliminate letters in a circular list for FLAMES, start at index 0, advance by (count - 1) positions using modulo arithmetic, remove the letter at that position, adjust the index, and repeat until 1 letter remains.
What is the time complexity of the FLAMES program?
The time complexity of the FLAMES program is O(n x m) for letter cancellation with a naive approach, or O(n + m) with frequency arrays. The elimination step is O(36) constant time. For typical name lengths of 3-20 characters, performance is negligible.
Why does my FLAMES program give a different result than the manual method?
A FLAMES program gives a different result than the manual method when the code cancels all matching characters at once instead of 1 pair at a time. Verify that the cancellation step removes only 1 occurrence from each name per match, not all occurrences.
How do you handle duplicate letters in the FLAMES game code?
Handle duplicate letters in the FLAMES game code by tracking matched characters with boolean flags or null markers, so each pair of matching letters cancels only 1 occurrence from each name.
Can I build a FLAMES calculator using HTML and JavaScript?
Yes, build a FLAMES calculator using HTML and JavaScript by creating 2 text input fields, 1 Calculate button, and 1 result display element, then calling the JavaScript flames() function on button click.
Is the FLAMES game a good beginner coding project?
Yes, the FLAMES game is a good beginner coding project for practicing 4 programming concepts: string manipulation, loop structures, array/list operations, and modular arithmetic.
What is the full form of FLAMES?
The full form of FLAMES is Friends, Lovers, Affection, Marriage, Enemies, Siblings. Each letter represents 1 of 6 relationship labels produced by the FLAMES algorithm.
Alex Carter
Creator & Quiz Researcher
Alex is a writer and internet culture researcher specializing in relationship quizzes, compatibility games, and digital nostalgia. Having grown up playing FLAMES in the back of school notebooks, Alex studies how folk games evolve into digital culture.
Read more about our team →More Articles
Does a FLAMES Calculator by Date of Birth Work Differently From the Name Version?
Discover whether a FLAMES calculator by date of birth works differently from the classic name game, how birthday compatibility mechanics work, and why results differ.
How Do You Use a FLAMES Calculator for Your Crush, and What Does the Result Mean?
Learn how to use a FLAMES calculator for your crush step by step, what each FLAMES result (F, L, A, M, E, S) means, which names to enter, and whether the FLAMES game predicts your crush's feelings.