How to resolve the algorithm Jewels and stones step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jewels and stones step by step in the JavaScript programming language

Table of Contents

Problem Statement

Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer. Both strings can contain any number of upper or lower case letters. However, in the case of 'jewels', all letters must be distinct. The function should count (and return) how many 'stones' are 'jewels' or, in other words, how many letters in 'stones' are also letters in 'jewels'.

Note that: So, for example, if passed "aAAbbbb" for 'stones' and "aA" for 'jewels', the function should return 3. This task was inspired by this problem.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jewels and stones step by step in the JavaScript programming language

Explanation:

Function:

  • jewelCount: This function counts the number of jewels in a given string.
    • Parameters:
      • j: A string containing the types of jewels to count.
      • s: A string containing the input string.
    • Returns: An integer representing the number of jewels in the input string.

Implementation:

  • The function splits the input string of jewels (j) into an array of individual characters (js).
  • It then splits the input string (s) into an array of individual characters.
  • The reduce method is used to iterate over each character in the input string and check if it is present in the array of jewels (js).
    • If the character is present, the accumulator (a) is incremented by 1.
    • Otherwise, the accumulator remains unchanged.
  • The initial value of the accumulator is set to 0, representing the initial count of jewels.

Test Cases:

  • The provided test cases are:
    • ["aA", "aAAbbbb"]: In this case, there are 3 jewels (a and 2 As) in the input string, so the result is 3.
    • ["z", "ZZ"]: In this case, there are 2 jewels (z and Z) in the input string, so the result is 2.

Usage:

The code is wrapped in an immediately invoked function expression (IIFE) to create a private scope. The function jewelCount is exported by returning the array of results from the test cases.

Source code in the javascript programming language

(() => {

    // jewelCount :: String -> String -> Int
    const jewelCount = (j, s) => {
        const js = j.split('');
        return s.split('')
            .reduce((a, c) => js.includes(c) ? a + 1 : a, 0)
    };

    // TEST -----------------------------------------------
    return [
            ['aA', 'aAAbbbb'],
            ['z', 'ZZ']
        ]
        .map(x => jewelCount(...x))
})();


  

You may also check:How to resolve the algorithm Exceptions step by step in the jq programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the GML programming language
You may also check:How to resolve the algorithm Weird numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm File modification time step by step in the Ring programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the PowerShell programming language