需要 3 个参数:订单列表 OrderList;订单 ID 号;& 一个“正在处理”或“已交付”的字符串状态。OrderList 是订单对象的数组。
根据状态更新订单列表并返回更新的列表。
-> 如果状态为“处理中”,则更新列表中 id 为 OrderId 的对象,使其状态为“处理中”。
-> 如果状态为“Delivered”,则从列表中删除 ID 为 orderId 的对象。
如果没有给定 orderId 的订单,则该函数将返回列表 orderList 不变。
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding("ascii");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", function(chunk) {
inputString += chunk;
});
process.stdin.on("end", function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
function processOrderList(orderList, orderId, state) {
// Write your code here
console.log(orderId);
console.log(orderList);
console.log(state);
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const orderCount = parseInt(readLine().trim());
let orderList = [];
for (let i = 0; i < orderCount; i++) {
orderList.push({
id: i + 1,
state: 'Received'
})
};
let numberOfOperations = parseInt(readLine().trim());
let updatedOrderList = [...orderList];
while (numberOfOperations-- > 0) {
const inputs = readLine().trim().split(' ');
const orderId = parseInt(inputs[0]);
const updatedState = inputs[1];
updatedOrderList = processOrderList(updatedOrderList, orderId, updatedState);
updatedOrderList = [...updatedOrderList];
}
if (updatedOrderList.length > 0) {
for (let i = 0; i < updatedOrderList.length; i++) {
const order = updatedOrderList[i];
ws.write(`Order with id ${order.id} is in ${order.state} state\n`);
};
} else {
ws.write(`All orders are in Delivered state\n`);
}
ws.end();
}Run Code Online (Sandbox Code Playgroud)
小智 5
这会好起来的
function processOrderList(orderList, orderId, state) {
// Write your code here
return state === 'Processing' ?
orderList.map(item => ({
...item,
state: item.id === orderId ? 'Processing' : item.state
})) :
orderList.filter(item => item.id !== orderId);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6325 次 |
| 最近记录: |