Count Items Matching a Rule
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"
andruleValue == typei
.ruleKey == "color"
andruleValue == colori
.ruleKey == "name"
andruleValue == 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
-
Write a function
countMatches
that takes arrayitems
, stringruleKey
, stringruleValue
, and returns the number of items that match the given rule. Export the function using ESM. -
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.
1import fs from 'fs' 2import { countMatches } from '/home/damner/code/index.js' 3 4// testlog is a log of test results 5const testlog = [] 6 7// this first block matches with - Challenge 1 8try { 9 const result = countMatches([["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], "color", "silver") 10 11 // perform some testing 12 if(result === 1) { 13 testlog.push({ status: 'pass' }) 14 } else { 15 throw new Error('Function countMatches is not working correctly') 16 } 17} catch(error) { 18 testlog.push({ 19 status: 'error', 20 error: error.message || 'Challenge 1 failed' 21 }) 22} 23 24// this second block matches with - Challenge 2 25try { 26 const result2 = countMatches([["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], "type", "phone") 27 28 // perform some testing 29 if(result2 === 2) { 30 testlog.push({ status: 'pass' }) 31 } else { 32 throw new Error('Function countMatches is not handling edge cases correctly') 33 } 34} catch(error) { 35 testlog.push({ 36 status: 'error', 37 error: error.message || 'Challenge 2 failed' 38 }) 39} 40 41// very important for the final length of `testlog` array to match the number of challenges, in this case - 2. 42 43// write the test log 44fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog)) 45 46// write the results array boolean. this will map to passed or failed challenges depending on the boolean value at the challenge index 47fs.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
1#!/bin/bash 2set -e 1 3 4mkdir -p /home/damner/code/.labtests 5 6cat > /home/damner/code/.labtests/package.json << EOF 7{ 8 "type": "module" 9} 10EOF 11 12cd /home/damner/code/.labtests 13mv $TEST_FILE_NAME ./nodecheck.test.js 14 15# import puppeteer doesn't work without it 16npm link puppeteer 17 18node 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']