Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. /*
  4.  * Create a function `multiply` that takes two number arguments
  5.  * and returns the result of the multiplication of those two.
  6.  * But you must do this without using the operators * or /
  7.  *
  8.  * @notions Primitive and Operators, Functions, While
  9.  */
  10.  
  11. // Your code :
  12.  
  13.  
  14. const multiply = (nb1 , nb2) => {
  15.  
  16. let j = 0
  17.  
  18.     if (nb1 === 0 || nb2 === 0)
  19.         return j
  20.     else if (nb1 < 0 && nb2 > 0)
  21.     {
  22.         return j = (- neg(nb1 , nb2))
  23.     }
  24.     else if (nb2 < 0 && nb1 > 0)
  25.     {
  26.         return j = (- neg(nb1 ,nb2))
  27.     }
  28.     else if (nb2 < 0 && nb1 < 0) {
  29.         const nb1neg = nb1 - nb1 - nb1
  30.         const nb2neg = nb2 - nb2 - nb2
  31.         return neg(nb1neg,nb2neg)
  32.     }
  33.     else   
  34.         return neg(nb1,nb2)
  35.  
  36.  
  37. }
  38.  
  39. const neg = (nb1,nb2) => {
  40.  
  41.  
  42. let j = 0
  43. let i = 0
  44.  
  45.     if (nb2 < 0 )
  46.     {
  47.         while (i < nb1)
  48.         {
  49.                 j = j + nb2
  50.                 i++
  51.         }
  52.  
  53.             return (j = j - j - j)
  54.     }
  55.     else if (nb1 < 0 )
  56.     {
  57.         while (i < nb2)
  58.         {
  59.                 j = j + nb1
  60.                 i++
  61.         }
  62.  
  63.             return (j = j - j - j)
  64.     }
  65.     else{
  66.         while (i < nb2)
  67.         {
  68.                 j = j + nb1
  69.                 i++
  70.         }      
  71.             return j
  72.         }
  73. }
  74.  
  75.  
  76.  
  77. //* Begin of tests
  78. const assert = require('assert')
  79.  
  80. assert.strictEqual(typeof multiply, 'function')
  81. assert.strictEqual(multiply.length, 2)
  82. assert.strictEqual(multiply.toString().includes('Math.imul'), false)
  83. assert.strictEqual(multiply.toString().includes('*'), false)
  84. assert.strictEqual(multiply.toString().includes('/'), false)
  85. assert.strictEqual(multiply(34, 78), 2652)
  86. assert.strictEqual(multiply(123, 0), 0)
  87. assert.strictEqual(multiply(0, -230), 0)
  88. assert.strictEqual(multiply(0, 0), 0)
  89. assert.strictEqual(multiply(123, -22), -2706)
  90. assert.strictEqual(multiply(-22, 123), -2706)
  91. assert.strictEqual(multiply(-22, -123), 2706)
  92. // End of tests */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement