NikolayBezay

Intership niko fail :(

Jun 11th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // 1. Write function to get random value in range from -100 to 100
  3. const random = (num1, num2) => {
  4.    return (Math.random() * (num2 - num1) + num1).toFixed(2);
  5. };
  6.  
  7. console.log('1: ', random(-100, 100))
  8.  
  9. // 2. Write function to check is positive random number,
  10. const isPositive = (number) => {
  11.     console.log('value: ', number);
  12.    return number > 0;
  13. };
  14. console.log('2: ', isPositive(random(-100, 100)));
  15. // output should be boolean
  16.  
  17. // 3. Write function, which create Array of random numbers. Array length = 6;
  18. // Result of function should be sum of array elements
  19. const ARRAY_LENGTH = 6;
  20. const getArraySum = (array) => {
  21.     for(var i=0; i < ARRAY_LENGTH; i++) {
  22.         array.push(parseFloat(random(-100, 100)));    
  23.     }
  24.     console.log(array);
  25.     return array.reduce((a, b) => a + b, 0);
  26. };
  27.  
  28. console.log('3:', getArraySum([]));
  29.  
  30. // 4. Write function to create random color
  31. const getRandomColor = () => {
  32.     const COLOR_MIN_VALUE = 0;
  33.     const COLOR_MAX_VALUE = 255;
  34.  
  35.     const getOneColor = ()  => { return parseInt(random(COLOR_MIN_VALUE, COLOR_MAX_VALUE))};
  36.     return `rgb(${getOneColor()}, ${getOneColor()}, ${getOneColor()})`;
  37. };
  38. console.log('4: ', getRandomColor());
  39. // output rgb(*, *, *);
  40.  
  41. // 5. Write function to find out if a number is a natural number or not
  42. const isNatural = (number) => {
  43.     if (typeof number !== 'number')
  44.         return 'Not a number';
  45.            
  46.      return (number >= 0.0) && (Math.floor(number) === number) && number !== Infinity;
  47. };
  48. let testValuesArray = [10, 22, -23, 5.4];
  49. testValuesArray.forEach((item) => {
  50.     console.log('5: ', item + ' is natural = ', isNatural(item));
  51. });
  52.  
  53.  
  54. // 6. Create function isDivideBy(number, firstDivider, secondDivider). Check if an integer number is divisible by each out of two arguments
  55.  
  56. /**
  57.  * (45, 1, 6)    ->  false
  58.  * (45, 5, 15)   ->  true
  59.  *
  60.  * (-12, 2, -6)  ->  true
  61.  * (-12, 2, -5)  ->  false
  62.  */
  63. const isDivideBy = (number, firstDivider, secondDivider) => {
  64.     return (number % firstDivider === 0 && number % secondDivider === 0);
  65. };
  66.  
  67. console.log('6: ', isDivideBy(45, 1, 6));
  68. console.log('6: ', isDivideBy(45, 5, 15));
  69. console.log('6: ', isDivideBy(-12, 2, -6));
  70. console.log('6: ', isDivideBy(-12, 2, -5));
  71.  
  72.  
  73. const obj = {
  74.     a: 1,
  75.     b: 2
  76. };
  77.  
  78. // 1. Log count of keys in object
  79. console.log('1: ', Object.keys(obj).length);
  80.  
  81. // 2. Create new object from "obj" and add to newly created param "c" with value "3".
  82. // Original "obj" shouldn't be modified
  83.  
  84. const obj2 = JSON.parse(JSON.stringify(obj));;
  85. obj2['c'] = 3;
  86.  
  87.  
  88. console.log(obj); // output {a: 1, b: 2};
  89. console.log(obj2); // should return {a: 1, b: 2, c: 3}
  90.  
  91. // 3.Check if is given input is array (Please, use more than one solution)
  92. const input1 = 'array';
  93. const input2 = ['array'];
  94.  
  95. console.log('3', Array.isArray(input1));
  96. console.log('3.1', Array.isArray(input2));
  97. console.log('3', input1.constructor === Array);
  98. console.log('3.1', input2.constructor === Array);
  99. console.log('3', input1 instanceof Array);
  100. console.log('3.1', input2 instanceof Array);
  101.  
  102.  
  103.  
  104. // 4. Remove duplicates from array (Try to find more than one solution)
  105. const array = ['A', 'B', 'A', 'C', 'B'];
  106.  
  107. console.log('4:', array.filter((value, index) => array.indexOf(value) === index));
  108. console.log('4:', [... new Set(array)]);
  109. console.log('4:', Array.from(new Set(array)));
  110.  
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment