kolesoffac

Tasks for interview

Apr 14th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // The rgb() method is incomplete. Complete the method so that passing in RGB decimal values will result in a hexadecimal representation being returned. The valid decimal values for RGB are 0 - 255. Any (r,g,b) argument values that fall out of that range should be rounded to the closest valid value.
  2.  
  3. //Note: Your answer should always be 6 characters long, the shorthand with 3 will not work here.
  4.  
  5. //The following are examples of expected output values:
  6.  
  7. //rgb(255, 255, 255) // returns FFFFFF
  8. //rgb(255, 255, 300) // returns FFFFFF
  9. //rgb(0,0,0) // returns 000000
  10. //rgb(148, 0, 211) // returns 9400D3
  11.  
  12. function rgb(r, g, b){
  13.   return toHex(r)+toHex(g)+toHex(b);
  14. }
  15.  
  16. function toHex(d) {
  17.     if(d < 0 ) {return "00";}
  18.     if(d > 255 ) {return "FF";}
  19.     return  ("0"+(Number(d).toString(16))).slice(-2).toUpperCase()
  20. }
  21.  
  22. -----
  23. let fill = c => (c.length === 1 ? ('0' + c) : c).toUpperCase();
  24. let check = n => n < 0 ? 0 : (n > 255 ? 255 : n);
  25. let convert = n => n.toString(16);
  26.  
  27. let rgb = (...args) => args.reduce((memo, item) => memo + fill(convert(check(item))), '');
  28. =================
  29. //Create a function named divisors/Divisors that takes an integer n > 1 and returns an array with all of the integer's //divisors(except for 1 and the number itself), from smallest to largest. If the number is prime return the string //'(integer) is prime' (null in C#) (use Either String a in Haskell and Result<Vec<u32>, String> in Rust).
  30.  
  31. //Example:
  32. //divisors(12); // should return [2,3,4,6]
  33. //divisors(25); // should return [5]
  34. //divisors(13); // should return "13 is prime"
  35.  
  36. function divisors(integer) {
  37.   let res = [];
  38.  
  39.   for (let i=2; i <= integer-1; i++) {
  40.      integer % i === 0 && res.push(i);
  41.   };
  42.  
  43.   return res.length ? res : (integer + " is prime");
  44. };
  45.  
  46. ==========================
  47. //Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary //representation of that number. You can guarantee that input is non-negative.
  48.  
  49. //Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case
  50.  
  51. var countBits = function(n) {
  52.   return n.toString(2).split('').filter(item => item === '1').length
  53. };
  54. ====================
  55. //Given an array of integers of any length, return an array that has 1 added to the value represented by the array.
  56.  
  57. //the array can't be empty
  58. //only non-negative, single digit integers are allowed
  59. //Return nil (or your language's equivalent) for invalid inputs.
  60.  
  61. //Examples
  62. //For example the array [2, 3, 9] equals 239, adding one would return the array [2, 4, 0].
  63.  
  64. //[4, 3, 2, 5] would return [4, 3, 2, 6]
  65.  
  66. function upArray(arr){
  67.   let numS = "";
  68.   let add = 0;
  69.   let index = 0;
  70.  
  71.   return arr.reduceRight((memo, item) => {
  72.     if (item < 0 || item > 10) {
  73.       memo = null;
  74.     };
  75.  
  76.     if (memo) {
  77.       if (index < 2) {
  78.         numS = item + numS;
  79.        
  80.         if (index === 1) {
  81.           numS = (parseInt(numS) + 1).toString();
  82.  
  83.           if (numS.length === 1) {
  84.             numS = "0" + numS;
  85.           } else if (numS.length === 3) {
  86.             numS = "00";
  87.             add = 1;
  88.           };
  89.          
  90.           memo.push(...numS.split('').map(i => parseInt(i)).reverse());
  91.         };
  92.       } else {
  93.         if (add) {
  94.           let newVar = item + add;
  95.          
  96.           if (newVar === 10) {
  97.             memo.push(0);
  98.           } else {
  99.             memo.push(newVar);
  100.             add = 0;
  101.           };
  102.         } else {
  103.           memo.push(item);
  104.         };
  105.       };
  106.      
  107.       index++;
  108.    
  109.       if (arr.length === index) {
  110.         add && memo.push(1);
  111.         memo = memo.reverse();
  112.       };
  113.     };
  114.    
  115.     return memo;
  116.   }, arr.length === 0 ? null : []);
  117. };
  118.  
  119. =========================
  120. //ROT13 is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. //ROT13 is an example of the Caesar cipher.
  121.  
  122. //Create a function that takes a string and returns the string ciphered with Rot13. If there are numbers or special //characters included in the string, they should be returned as they are. Only letters from the latin/english alphabet //should be shifted, like in the original Rot13 "implementation".
  123.  
  124. function rot13(message){
  125.   let alphabetAll = [65, 97].map(beginCode => [...Array(26).keys()].map(n => n + beginCode).map(n => String.fromCharCode(n)));
  126.   let alphabet = alphabetAll.reduce((memo, item) => [...memo, ...item, ...item.slice(0, 13)], []);
  127.  
  128.   return message.split('').reduce((memo, item) => (memo += (pos = alphabet.indexOf(item)) != -1 ? alphabet[pos + 13] : item), '');
  129. };
  130.  
  131. ==============================
  132.  
  133. function add(n) {
  134. if (typeof add.summ === "number") {
  135.     add.summ += n;
  136. } else {
  137.     add.summ = n
  138.  };
  139.  
  140.  return add;
  141. }
  142.  
  143. Function.prototype.toString = function () {
  144. var res = this.summ || "No sum";
  145.  
  146. delete this.summ;
  147.  
  148. return res;
  149. }
  150.  
  151. alert( add(1) ) // выведет '1'
  152. alert( add(1)(9) ) // выведет '10'
  153. alert( add(4)(4)(4) ) // выведет '12'
Advertisement
Add Comment
Please, Sign In to add comment