• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# calculations

justFiona Jan 15th, 2016 (edited) 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. <script>
2.
3. var inputExprArray = "1+3*3/5*8*9+7-2*2";
4.
5. var signArr = inputExprArray.split(/[(0-9),*,\/]+/);
6. var numbArr = inputExprArray.split(/[+,\-]+/)
7.
8. console.log(signArr);
9. console.log(numbArr);
10.
11.
12.
13. var newArray = numbArr.map(function multiplication(input){
14. console.log(input);
15. if(input.search(/[*,\/]+/) == -1){return input;}
16. else{
17. var multipl = input.split(/[0-9]+/);
18.     var numbers = input.split(/[*,\/]+/);
19.     multipl.pop();
20.
21.     console.log(multipl);
22.
23.     console.log(numbers);
24.
25. var multOfArr = numbers.reduce(function(previousValue, currentValue, currentIndex, array) {
26.     if (multipl[currentIndex] == ""  ){
27.   return parseFloat(currentValue)* 1; }
28.   else if(multipl[currentIndex] == "*"  ){return parseFloat(previousValue) * parseFloat(currentValue);}
29.
30. else{return parseFloat(previousValue) *(1/ parseFloat(currentValue));}
31.
32. });
33. return multOfArr;}
34. console.log(multOfArr);
35.
36. }
37.     );
38. console.log(newArray);
39. var lastArr = newArray.reduce(function(previousValue, currentValue, currentIndex, array) {
40.     if (signArr[currentIndex] == "" ||signArr[currentIndex] == "+" ){
41.   return parseFloat(previousValue)+ parseFloat(currentValue); }
42.   else {return parseFloat(previousValue) - parseFloat(currentValue);}
43. });
44.
45.
46. console.log(lastArr);
47. </script>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top