# Count Items Matching a Rule

Easy
14
2
45.5% Acceptance

In this lab, you will be implementing a function called `countMatches` that takes two inputs: an array `items`, where each `items[i] = [typei, colori, namei]` describes the type, color, and name of the `ith` item, and a rule represented by two strings, `ruleKey` and `ruleValue`.

The function should return the number of items that match the given rule. An item is said to match the rule if one of the following is true:

• `ruleKey == "type"` and `ruleValue == typei`.
• `ruleKey == "color"` and `ruleValue == colori`.
• `ruleKey == "name"` and `ruleValue == namei`.

Example 1:

``countMatches([["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], "color", "silver")``

Output: 1

Explanation: There is only one item matching the given rule, which is `["computer","silver","lenovo"]`.

Example 2:

``countMatches([["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], "type", "phone")``

Output: 2

Explanation: There are only two items matching the given rule, which are `["phone","blue","pixel"]` and `["phone","gold","iphone"]`. Note that the item `["computer","silver","phone"]` does not match.

### Constraints:

• `1 <= items.length <= 104`
• `1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10`
• `ruleKey` is equal to either `"type"`, `"color"`, or `"name"`.
• All strings consist only of lowercase letters.

## Challenges

1. Write a function `countMatches` that takes array `items`, string `ruleKey`, string `ruleValue`, and returns the number of items that match the given rule. Export the function using ESM.

2. Ensure that your function has proper error handling and check for edge cases.

## Evaluation Script

You'll test your solution using the following evaluation script, in which two challenges are tested. To pass the lab, make sure your code is well-formatted, error-free, and follows the constraints and guidelines mentioned above.

``````import fs from 'fs'
import { countMatches } from '/home/damner/code/index.js'

// testlog is a log of test results
const testlog = []

// this first block matches with - Challenge 1
try {
const result = countMatches([["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], "color", "silver")

// perform some testing
if(result === 1) {
testlog.push({ status: 'pass' })
} else {
throw new Error('Function countMatches is not working correctly')
}
} catch(error) {
testlog.push({
status: 'error',
error: error.message || 'Challenge 1 failed'
})
}

// this second block matches with - Challenge 2
try {
const result2 = countMatches([["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], "type", "phone")

// perform some testing
if(result2 === 2) {
testlog.push({ status: 'pass' })
} else {
throw new Error('Function countMatches is not handling edge cases correctly')
}
} catch(error) {
testlog.push({
status: 'error',
error: error.message || 'Challenge 2 failed'
})
}

// very important for the final length of `testlog` array to match the number of challenges, in this case - 2.

// write the test log
fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog))

// write the results array boolean. this will map to passed or failed challenges depending on the boolean value at the challenge index
fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')))``````

Remember to export the `countMatches` function in your code using ESM. Follow the guidelines and constraints in the lab description, and ensure that your code works perfectly with the evaluation script above.

### Setting up Test Environment Script

``````#!/bin/bash
set -e 1

mkdir -p /home/damner/code/.labtests

cat > /home/damner/code/.labtests/package.json << EOF
{
"type": "module"
}
EOF

cd /home/damner/code/.labtests
mv \$TEST_FILE_NAME ./nodecheck.test.js

# import puppeteer doesn't work without it

node nodecheck.test.js 2>&1 | tee evaluationscript.log``````

### Initial File System for User

``````// index.js
/**
* @param {string[][]} items
* @param {string} ruleKey
* @param {string} ruleValue
* @return {number}
*/
export const countMatches = function(items, ruleKey, ruleValue) {

};``````
``````// package.json
{
"name": "codedamn-lab",
"type": "module"
}``````
``````tabs: ['index.js']
terminals: ['yarn install']``````