Skip to the code
cf.
+10 XP

#739 Are Two Settings Equal?

easy

A settings page enables its Save button only when the edited settings differ from the saved ones. The settings are nested JSON values, and === on two objects only checks whether they are the same object.

Write deepEqual(a, b) that returns true when the two JSON values have the same contents: equal primitives (null, numbers, strings, booleans), arrays of the same length with deep-equal items in the same order, or objects with the same set of keys and deep-equal values (key order doesn't matter). An array never equals an object.

Example 1

Input: a = {"theme":"dark","tabs":[1,2],"font":{"size":14}}, b = {"font":{"size":14},"tabs":[1,2],"theme":"dark"}
Output: true

Same keys and values; the key order differs, which doesn't matter.

Example 2

Input: a = {"tabs":[1,2]}, b = {"tabs":[2,1]}
Output: false

Array order matters.

Constraints

  • Values are valid JSON with at most 10^4 items in total and nesting up to 100 levels.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).