Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ops = ["5", "2", "C", "D", "+"]
- const intructionset = {
- "N": (n) => {
- return parseInt(n)
- },
- "+": (array) => {
- return [...array, array[array.length - 1] + array[array.length - 2]]
- },
- "D": (array) => {
- return [...array, array[array.length - 1] * 2]
- },
- "C": (array) => {
- return array.filter((ele, index) => index != array.length - 1)
- },
- }
- const record = ops.reduce((recordAccumulator, currentInstruction) => {
- if (currentInstruction !== 'D' && currentInstruction !== 'C' && currentInstruction !== '+') {
- return [...recordAccumulator, intructionset["N"](currentInstruction)]
- }
- return intructionset[currentInstruction](recordAccumulator)
- }, []).reduce((sum, v) => sum + v, 0)
- console.log(ops)
- console.log(record)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement