🎃 Spooky Challenge - Day 6 🎃
The Bewitched Balance
In the enchanted forest, there's a magical scale used by the ancient druids. This scale, unlike any other, doesn't weigh objects, but rather the characters in strings. Legends say that if balanced correctly, the scale reveals a number.
A whispered chant from the forest provides a clue:
With letters and numbers, the scale does play,
Find the balance, and you'll see the way.
If the weight's the same, left and right,
The scale will show zero, shining bright.
The Challenge:
You're given a string and a function:
Your task is to fill in the findBalance function. It should check if the number of alphabetic characters (a-z, A-Z) is equal to the number of digits (0-9) in the string s. If they are equal, return the number 0. Otherwise, return -1.
Hint: Regular expressions can be a powerful tool for this challenge.
🔗 Useful Resource: To master the art of string manipulation in JavaScript, W3Schools' JavaScript String Methods could be your guiding star.
🌐 Platform Suggestion: To craft your spells and unveil the magic of the scale, JSFiddle or Repl.it is recommended.
The Bewitched Balance
In the enchanted forest, there's a magical scale used by the ancient druids. This scale, unlike any other, doesn't weigh objects, but rather the characters in strings. Legends say that if balanced correctly, the scale reveals a number.
A whispered chant from the forest provides a clue:
With letters and numbers, the scale does play,
Find the balance, and you'll see the way.
If the weight's the same, left and right,
The scale will show zero, shining bright.
The Challenge:
You're given a string and a function:
JavaScript:
let str = "ab01";
function findBalance(s) {
// Your code here
return -1; // If not balanced
}
Your task is to fill in the findBalance function. It should check if the number of alphabetic characters (a-z, A-Z) is equal to the number of digits (0-9) in the string s. If they are equal, return the number 0. Otherwise, return -1.
Hint: Regular expressions can be a powerful tool for this challenge.
🔗 Useful Resource: To master the art of string manipulation in JavaScript, W3Schools' JavaScript String Methods could be your guiding star.
🌐 Platform Suggestion: To craft your spells and unveil the magic of the scale, JSFiddle or Repl.it is recommended.
Mystical coder of the enchanted forest, you've deciphered the riddle of the bewitched balance!
By invoking the power of regular expressions, you've determined the balance between letters and numbers. For the string "ab01", the scale shows a balance, thus revealing the number: 0. Your wisdom and skill have brought balance to the forest once more.
JavaScript:
function findBalance(s) {
// Count the number of alphabetic characters using a regular expression
let letters = s.match(/[a-zA-Z]/g);
let letterCount = letters ? letters.length : 0;
// Count the number of digits using a regular expression
let numbers = s.match(/\d/g);
let numberCount = numbers ? numbers.length : 0;
// Compare the counts and return the result
return letterCount === numberCount ? 0 : -1;
}
// Test the function with the provided string "ab01"
let testString = "ab01";
let result = findBalance(testString);
console.log(result); // Expected output: 0
By invoking the power of regular expressions, you've determined the balance between letters and numbers. For the string "ab01", the scale shows a balance, thus revealing the number: 0. Your wisdom and skill have brought balance to the forest once more.
Last edited: