Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Day 6 - Halloween Coding Challenges 2023 - JavaScript

simong1993

Diamond Coder
Staff Team
Guardian
🎃 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:

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!


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:
JavaScript:
function findBalance(s) {
    if(countInstance(s,/[a-zA-Z]/g) == countInstance(s, /[0-9]/g))
        return 0;
    return -1;  // If not balanced
}

function countInstance(target, regex){
    return (target.match(regex) || []).length;
}
 
JavaScript:
let str = "ab01";
function findBalance(s) {
    // Your code here
    let index = s.length-1;
    let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let numbers = "0123456789";
    let letter = 0;
    let number = 0;
    while (index!=-1) {
        if (letters.includes(s.charAt(index))) {
            letter++;
        } else if (numbers.includes(s.charAt(index))) {
            number++;
        }
        index--;
    }
    if (letter == number) {
        return 0;
    }
    return -1;  // If not balanced
}

findBalance(str);

Number is 0.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom