Counting Magical Crystals

You are an apprentice wizard exploring a mystical cave filled with magical crystals of various colors. Your task is to count all the crystals of a specific color to complete your spell.

Write a function named countCrystals that takes two parameters:

  • crystals: An array of strings where each element represents the color of a crystal.

  • color: A string representing the color of crystals you want to count.

Example 1

Input:
crystals = ["blue", "blue", "blue", "blue", "blue"];
color = "blue"

Output: 5

Example 2

Input:
crystals = ["green", "green", "yellow", "yellow", "green"];
color = "green"

Output: 3

Example 3

Input:

crystals = ["red", "red", "red", "red"];
color = "blue"

Output: 0

Are you stuck on this problem?

Get detailed, step-by-step solution and improve your JavaScript skills.

  • Detailed Explanation: Each solution comes with a comprehensive explanation, helping you understand the logic and concepts thoroughly.
  • Learn to Write Better Code: Even if you have written the code, discover ways to improve it and adopt best practices.
  • Save Time: Don’t waste hours struggling—get the solution and learn efficiently.
  • Lifetime Access: Get access to lifetime solutions for all problems available
Get the Solution

" I struggled with this problem for hours, but the solution provided here was clear and easy to follow. It helped me understand where I went wrong and improved my coding skills significantly." - Jane D.

If you're not satisfied, I offer a 7-day money-back guarantee.