give a js function which can print a js object with keys and counts. the function should…
- User
give a js function which can print a js object with keys and counts. the function should print one final string.
Assistant
javascriptfunction printObjectKeysAndCounts(obj) { if (obj === null || typeof obj !== 'object') { return "Invalid input: Expected an object"; } const keyCounts = {}; // Count keys for (const key in obj) { if (obj.hasOwnProperty(key)) { keyCounts[key] = (keyCounts[key] || 0) + 1; } } // Format output string let result = ""; for (const key in keyCounts) { if (result) result += ", "; result += `${key}: ${keyCounts[key]}`; } return result || "Empty object"; }Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.