Write a function. Test real cases. Improve your solution.
Given an items array with unique sku strings and positive integer quantity fields, update the item matching sku. A positive new quantity replaces its quantity; zero removes the item. An unknown sku leaves the contents unchanged. Preserve item order and every unrelated field, including nested data. Do not mutate the input array or its objects. Returning the original array or unchanged objects is allowed when the operation changes no values; copy an object when its quantity changes. The supplied new quantity is always a nonnegative integer.
[
[
{
"sku": "keyboard",
"quantity": 1,
"price": 80
},
{
"sku": "mouse",
"quantity": 2,
"price": 25
}
],
"mouse",
4
][
{
"sku": "keyboard",
"quantity": 1,
"price": 80
},
{
"sku": "mouse",
"quantity": 4,
"price": 25
}
][
[
{
"sku": "a",
"quantity": 1
},
{
"sku": "b",
"quantity": 2
},
{
"sku": "c",
"quantity": 3
}
],
"b",
0
][
{
"sku": "a",
"quantity": 1
},
{
"sku": "c",
"quantity": 3
}
][
[
{
"sku": "a",
"quantity": 1
}
],
"missing",
5
][
{
"sku": "a",
"quantity": 1
}
][ [], "a", 1 ]
[]
[
[
{
"sku": "a",
"quantity": 1
}
],
"a",
0
][]
[
[
{
"sku": "book",
"quantity": 2,
"metadata": {
"edition": 3,
"tags": [
"paper"
]
}
}
],
"book",
7
][
{
"sku": "book",
"quantity": 7,
"metadata": {
"edition": 3,
"tags": [
"paper"
]
}
}
][
[
{
"sku": "USB",
"quantity": 1
},
{
"sku": "usb",
"quantity": 2
}
],
"USB",
3
][
{
"sku": "USB",
"quantity": 3
},
{
"sku": "usb",
"quantity": 2
}
][
[
{
"sku": "a",
"quantity": 2,
"price": 8
}
],
"a",
2
][
{
"sku": "a",
"quantity": 2,
"price": 8
}
][
[
{
"sku": "a",
"quantity": 2
}
],
"missing",
0
][
{
"sku": "a",
"quantity": 2
}
]Your code runs in your browser. These exercises use synchronous JavaScript functions and built-in data types; DOM access, network requests, and npm imports are unavailable. Runs stop after about four seconds. Test results support self-study and don’t certify proficiency.