kaburen

lab1

Oct 1st, 2020 (edited)
421
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const _ = require('lodash');
  2. const Calculator = require('./Calculator.js')
  3.  
  4. function sum1() {
  5.   sum = 0;
  6.   for(i = 0; i < arguments.length;i++){
  7.     sum += arguments[i];    
  8.   }
  9.   console.log(sum);
  10. }
  11. sum1(1,2,3,4,5);
  12. sum1(2,4,6);
  13.  
  14. function sum2() {  
  15.   sum = _.sum(arguments);
  16.   console.log(sum);
  17. }
  18. sum2(1,2,3,4,5);
  19.  
  20. function sumProt() {
  21.   let err = false;
  22.   let sum = 0;  
  23.   for(i = 0; i < arguments.length;i++){
  24.     if(_.isString(arguments[i]) || _.isObject(arguments[i])) {
  25.         err = true;        
  26.         console.log("Podano błędny argument: "+ arguments[i] + " na pozycji: "+ (i+1));        
  27.     }  
  28.       sum += arguments[i];
  29.   }
  30.   if(!err){
  31.     console.log(sum);
  32.   }
  33. }
  34. sumProt(1,"text",3,{},5);
  35.  
  36. console.log("====================================================")
  37.  
  38. const item1 = new Calculator([1, 2, 3, 4, 5])
  39. const item2 = new Calculator([1, "text", 3, {}])
  40.  
  41. item1.sum()
  42. item1.substract()
  43.  
  44. item2.sum()
  45. item2.substract()
  46. ===========================================================================
  47. const _ = require('lodash');
  48. module.exports = class Calculator {
  49.   constructor(items) {  
  50.    
  51.     this.items= _.filter(items, function(o) {
  52.       return !(_.isString(o) || _.isObject(o))})      
  53.       for(let i = 0; i < items.length;i++) {
  54.       if(_.isString(items[i]) || _.isObject(items[i])) {          
  55.         console.log("Podano błędny argument: "+ items[i] + " na pozycji: "+ (i+1));        
  56.       }
  57.     }          
  58.   }
  59.  
  60.   sum(){
  61.     var sum = _.reduce(this.items, function(sum, n) {
  62.       return sum + n;}, 0);
  63.     console.log("Suma wartosci ["+this.items+"] "+"wynosi: " +sum);  
  64.     // return sum
  65.   }
  66.  
  67.   substract() {
  68.       var sub = _.reduce(this.items, function(sub, n) {
  69.         return sub - n;});
  70.       console.log("Różnica z wartosci ["+this.items+"] "+"wynosi: " +sub);  
  71.       // return sub
  72.   }
  73. }
RAW Paste Data