Advertisement
PastebinhoLuisinho

Untitled

Jul 19th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. const ops = ["5", "2", "C", "D", "+"]
  2.  
  3. const intructionset = {
  4. "N": (n) => {
  5. return parseInt(n)
  6. },
  7. "+": (array) => {
  8. return [...array, array[array.length - 1] + array[array.length - 2]]
  9. },
  10. "D": (array) => {
  11. return [...array, array[array.length - 1] * 2]
  12. },
  13. "C": (array) => {
  14. return array.filter((ele, index) => index != array.length - 1)
  15. },
  16. }
  17.  
  18.  
  19. const record = ops.reduce((recordAccumulator, currentInstruction) => {
  20. if (currentInstruction !== 'D' && currentInstruction !== 'C' && currentInstruction !== '+') {
  21. return [...recordAccumulator, intructionset["N"](currentInstruction)]
  22. }
  23. return intructionset[currentInstruction](recordAccumulator)
  24. }, []).reduce((sum, v) => sum + v, 0)
  25.  
  26.  
  27.  
  28. console.log(ops)
  29. console.log(record)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement