Skip to content

Instantly share code, notes, and snippets.

@oleggrishechkin
Last active July 6, 2022 21:18
Show Gist options
  • Save oleggrishechkin/002706c9ccd79ddeab7ed5fac5cfe8dc to your computer and use it in GitHub Desktop.
Save oleggrishechkin/002706c9ccd79ddeab7ed5fac5cfe8dc to your computer and use it in GitHub Desktop.

This function sum all numbers in string with this format:

2000 - apples
4000 - oranges
1000 - potatos

let sumNumbers = (value) => value.split('\n').map((el) => el.split(' ')).flat().reduce((res, el) => !isNaN(+el) ? res + +el : res, 0);

Copy this funtion to browser console and run:

sumNumbers('your long string with numbers');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment