Problem statement
Write function solution(actions). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. actions is an array of {type:string,id:string,title?:string}. Start with an empty list. add inserts {id,title:trimmed title,completed:false} only when id is not an empty string, the trimmed title is nonempty, and the ID is absent. Ignore duplicate adds. update changes an existing title after trimming; ignore blank titles. toggle reverses completed for an existing task; remove deletes it. Ignore missing IDs and unknown types. Return current tasks in insertion order. A removed ID that is added again goes to the end. Input limits: at most 50,000 items per input array and 1,000 characters per string. Numbers are finite; integers and accumulated integer results stay in the JavaScript safe-integer range. Other types and validity rules follow the statement above.
Example input and output
Example 1
Function arguments
[
[
{
"type": "add",
"id": "a",
"title": " Write "
}
]
]Return value
[
{
"id": "a",
"title": "Write",
"completed": false
}
]Example 2
Function arguments
[
[
{
"type": "add",
"id": "a",
"title": "A"
},
{
"type": "toggle",
"id": "a"
}
]
]Return value
[
{
"id": "a",
"title": "A",
"completed": true
}
]Example 3
Function arguments
[ [] ]
Return value
[]