Creative Name Looping
"It's okay to take breaks, but don't give up. Let's get back on track!"
Challenge Description
Create a program that:
- Prints your name
n
times (user input) - Adds numbering in front
1. Syifa Fauziyah Arizal
2. Syifa Fauziyah Arizal
3. Syifa Fauziyah Arizal
What is N?
Here, n
means the number of repetitions requested by the user. So the user will be asked how many times they want to print their name.
Simple? Yep. But my brain lagged a bit because it's been a while since I worked with JS. 😅
First Attempt
const input = prompt("How many times do you want to print your name? ");
for (let i = 1; i <= input; i++) {
console.log(i + ". " + "Syifa Fauziyah Arizal");
}
Does it work? Yup! But not very robust yet.
Input Validation Version
const input = Number(prompt("How many times do you want to print your name? "));
if (!isNaN(input) && input > 0) {
for (let i = 1; i <= input; i++) {
console.log(`${i}. Syifa Fauziyah Arizal`);
}
} else {
console.log("Please enter a valid number 😅");
}
Explanation:
isNaN()
means is Not a Number, so we use!isNaN()
to prevent the loop from running if the user enters 0 or negative numbers (why would you want to print your name 0 times anyway? 😆).
Bonus Challenge
Level up! Add:
- Different emoji for each line (looping from array)
- Different motivational quote for each line
- Combine all outputs into one concatenated string
Example Output:
1. Syifa 🔥 — Keep going!
2. Syifa 💡 — You're learning!
3. Syifa 🌱 — Progress every day!
Solution: Using Arrays and Modulus
const emojis = ["🔥", "💡", "🌱", "🚀", "🎯", "💪", "🧠", "✨", "🔧", "🎉"];
const motivation = [
"Keep going!",
"You can do it!",
"Believe in yourself!",
"Stay positive!",
"Never give up!",
"Dream big!",
"Stay focused!",
"Work hard!",
"Embrace challenges!",
"Success is yours!",
];
Why Use %
(Modulus)?
For example if i = 11
, then emoji[11 % 10] = emoji[1]
means it will cycle back to the start of the array when the index exceeds the length. A genius way to create infinite looping within array bounds 💡.
✅ Simple, efficient, and creates creative outputs without errors!
Final Code
const input = Number(prompt("How many times do you want to print your name? "));
const emojis = ["🔥", "💡", "🌱", "🚀", "🎯", "💪", "🧠", "✨", "🔧", "🎉"];
const motivation = [
"Keep going!",
"You can do it!",
"Believe in yourself!",
"Stay positive!",
"Never give up!",
"Dream big!",
"Stay focused!",
"Work hard!",
"Embrace challenges!",
"Success is yours!",
];
let result = "";
if (!isNaN(input) && input > 0) {
for (let i = 0; i < input; i++) {
const emoji = emojis[i % emojis.length];
const quote = motivation[i % motivation.length];
result += `${i + 1}. Syifa ${emoji} - ${quote}\n`;
}
console.log(result);
} else {
console.log("Please enter a valid number 😅");
}
Mini Reflection
I got stuck at first, but after some tinkering, researching, and giving my brain some breathing room, I figured it out.
This challenge, while seemingly light, reminded me of the importance of basics like:
- ✔️ Input validation
- ✔️ Array looping with modulus
- ✔️ Combining strings with newline
\n
Sometimes we focus too much on big things, when it's these small things that form our foundation. And yes, I'm back. Let's go!
"When stuck, don't panic. Take a break first. Drink some water. Take a short walk. Then come back to coding. Your brain needs refreshing too."