最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Order List Processing - Stack Overflow

programmeradmin2浏览0评论

Takes 3 Arguments: an order list OrderList; a Number OrderId; & a String State that is either "Processing" or "Delivered". OrderList is an array of order objects.

Updates The order List Depending on the state and returns the Updated List.

-> If the state is "Processing", It updates the object in the list Having id as OrderId, to have the state "Processing".

-> If the state is "Delivered", It Deletes the object from the list having the id of orderId.

If there is no order with the given orderId then the Function returns the list orderList unchanged.

'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();
}

Takes 3 Arguments: an order list OrderList; a Number OrderId; & a String State that is either "Processing" or "Delivered". OrderList is an array of order objects.

Updates The order List Depending on the state and returns the Updated List.

-> If the state is "Processing", It updates the object in the list Having id as OrderId, to have the state "Processing".

-> If the state is "Delivered", It Deletes the object from the list having the id of orderId.

If there is no order with the given orderId then the Function returns the list orderList unchanged.

'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();
}

Share Improve this question asked Sep 6, 2021 at 15:42 deveopdeveop 1191 silver badge6 bronze badges 1
  • What is the question? – Jörg W Mittag Commented Sep 6, 2021 at 15:45
Add a ment  | 

1 Answer 1

Reset to default 5

This will be fine

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);
}
发布评论

评论列表(0)

  1. 暂无评论