Advertisement
boris-ivanov

Untitled

Jul 4th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1.  
  2.  
  3. function checkIfNumber(symbol){
  4. if(typeof symbol=="number"){
  5. return true
  6. }else{
  7. return false;
  8. }
  9. }
  10.  
  11.  
  12. function add (...items){
  13. arr=[];
  14. for (const item of items) {
  15. if(checkIfNumber(item)!=true) {
  16. throw new Error(`${item} is not a number`);
  17. }else{
  18. arr.push(item);
  19. }
  20. }
  21. const reducer = (accumulator, currentValue) => accumulator + currentValue;
  22. console.log(arr.reduce(reducer))
  23. }
  24.  
  25.  
  26.  
  27. function multy (...items){
  28. arr=[];
  29. for (const item of items) {
  30. if(checkIfNumber(item)!=true) {
  31. throw new Error(`${item} is not a number`);
  32. }else{
  33. arr.push(item);
  34. }
  35. }
  36. const reducer = (accumulator, currentValue) => accumulator * currentValue;
  37. console.log(arr.reduce(reducer))
  38. }
  39.  
  40.  
  41. const Calculator = (a,b,...Func) => {
  42. let arr = [];
  43. for(const item of Func)
  44. {
  45. arr.push(item(a,b));
  46. }
  47. return arr;
  48. };
  49. try{
  50. Calculator(2,5,multy,add,add);
  51. }catch(error)
  52. {
  53. console.log(error.message);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement