Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. function add(a, b) {
  2. return a + b;
  3. }
  4.  
  5. const newAdd = (a, b) => a + b;
  6.  
  7. const operate = (op1, op2, operation) => operation(op1, op2)
  8.  
  9. // console.log(operate(5, 10, (a, b) => a + b))
  10.  
  11. const arrayOperation = (arr, opOnEle) => {
  12. for (i = 0; i < arr.length; i++) {
  13. opOnEle(arr[i])
  14. }
  15. }
  16.  
  17. const arr1 = [1, 10, 20, 30, 40];
  18. // arrayOperation([10, 20, 30], (ele) => console.log(ele))
  19.  
  20. // arr1.forEach((ele) => console.log(ele))
  21.  
  22. // ----------------------------------
  23. // [1,2,3,4] -> [1,4,9,16]
  24. const mapOperation = (arr, operation) => {
  25. const result = []
  26. for (i = 0; i < arr.length; i++) {
  27. const res = operation(arr[i])
  28. result.push(res)
  29. }
  30. return result
  31. }
  32.  
  33. const filterOperation = (arr, condition) => {
  34. const result = [];
  35. for (i = 0; i < arr.length; i++) {
  36. const r = condition(arr[i])
  37. if (r) {
  38. result.push(arr[i])
  39. }
  40. }
  41. return result;
  42. }
  43. // console.log(filterOperation(arr1, (ele) => ele % 2 == 0))
  44.  
  45.  
  46. // console.log(mapOperation(arr1, (ele) => ele * ele))
  47. // console.log(mapOperation(arr1, (ele) => ele * ele * ele))
  48. // console.log(arr1.map((ele) => ele * ele))
  49. // console.log(arr1.filter((e) => e % 2 == 0))
  50.  
  51.  
  52. const student = {
  53. name: "mahadev",
  54. college: "jspm",
  55. roll: 10
  56. }
  57. // console.log(student["name"])
  58.  
  59. // find all students of JSPM college and having marks
  60. // greater than 20% and return rollNumbers of them
  61.  
  62. const students = [
  63. {
  64. name: "mahadev",
  65. college: "jspm",
  66. roll: 10,
  67. marks: 30
  68. },
  69. {
  70. name: "mahadev1",
  71. college: "BSIOTR",
  72. roll: 11,
  73. marks: 20
  74. },
  75. {
  76. name: "mahadev2",
  77. college: "jspm",
  78. roll: 12,
  79. marks: 10
  80. }
  81. ]
  82.  
  83. students.filter(s => s.college == "jspm")
  84.  
  85. const rolls = students.filter((student) => student.college == "jspm")
  86. .filter((student) => student.marks > 20)
  87. .map(student => student.roll)
  88.  
  89. // console.log(rolls)
  90.  
  91. const task = (success, failure) => {
  92. console.log("data is performing certaion operation")
  93. console.log("searching for mobile num,ber")
  94. setTimeout(() => {
  95. console.log("task got failed ")
  96. const resultOfTask = 9096969220;
  97. failure(resultOfTask)
  98. }, 1000)
  99. }
  100. const dattasPromise = new Promise(task)
  101. const suuccessCallback = (result) => {
  102. console.log("I got mobile number from datta", result)
  103. console.log("calloing that numnber")
  104. }
  105. const failureCallback = (err) => {
  106. console.log("Datta has failed to perform operation and here is error: ", err)
  107. console.log("I may try to do some other way")
  108. }
  109. dattasPromise.then(suuccessCallback).catch(failureCallback)
  110. console.log("assigned operation to datta and continueing my other work")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement