Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- //Note: Your answer should always be 6 characters long, the shorthand with 3 will not work here.
- //The following are examples of expected output values:
- //rgb(255, 255, 255) // returns FFFFFF
- //rgb(255, 255, 300) // returns FFFFFF
- //rgb(0,0,0) // returns 000000
- //rgb(148, 0, 211) // returns 9400D3
- function rgb(r, g, b){
- return toHex(r)+toHex(g)+toHex(b);
- }
- function toHex(d) {
- if(d < 0 ) {return "00";}
- if(d > 255 ) {return "FF";}
- return ("0"+(Number(d).toString(16))).slice(-2).toUpperCase()
- }
- -----
- let fill = c => (c.length === 1 ? ('0' + c) : c).toUpperCase();
- let check = n => n < 0 ? 0 : (n > 255 ? 255 : n);
- let convert = n => n.toString(16);
- let rgb = (...args) => args.reduce((memo, item) => memo + fill(convert(check(item))), '');
- =================
- //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).
- //Example:
- //divisors(12); // should return [2,3,4,6]
- //divisors(25); // should return [5]
- //divisors(13); // should return "13 is prime"
- function divisors(integer) {
- let res = [];
- for (let i=2; i <= integer-1; i++) {
- integer % i === 0 && res.push(i);
- };
- return res.length ? res : (integer + " is prime");
- };
- ==========================
- //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.
- //Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case
- var countBits = function(n) {
- return n.toString(2).split('').filter(item => item === '1').length
- };
- ====================
- //Given an array of integers of any length, return an array that has 1 added to the value represented by the array.
- //the array can't be empty
- //only non-negative, single digit integers are allowed
- //Return nil (or your language's equivalent) for invalid inputs.
- //Examples
- //For example the array [2, 3, 9] equals 239, adding one would return the array [2, 4, 0].
- //[4, 3, 2, 5] would return [4, 3, 2, 6]
- function upArray(arr){
- let numS = "";
- let add = 0;
- let index = 0;
- return arr.reduceRight((memo, item) => {
- if (item < 0 || item > 10) {
- memo = null;
- };
- if (memo) {
- if (index < 2) {
- numS = item + numS;
- if (index === 1) {
- numS = (parseInt(numS) + 1).toString();
- if (numS.length === 1) {
- numS = "0" + numS;
- } else if (numS.length === 3) {
- numS = "00";
- add = 1;
- };
- memo.push(...numS.split('').map(i => parseInt(i)).reverse());
- };
- } else {
- if (add) {
- let newVar = item + add;
- if (newVar === 10) {
- memo.push(0);
- } else {
- memo.push(newVar);
- add = 0;
- };
- } else {
- memo.push(item);
- };
- };
- index++;
- if (arr.length === index) {
- add && memo.push(1);
- memo = memo.reverse();
- };
- };
- return memo;
- }, arr.length === 0 ? null : []);
- };
- =========================
- //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.
- //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".
- function rot13(message){
- let alphabetAll = [65, 97].map(beginCode => [...Array(26).keys()].map(n => n + beginCode).map(n => String.fromCharCode(n)));
- let alphabet = alphabetAll.reduce((memo, item) => [...memo, ...item, ...item.slice(0, 13)], []);
- return message.split('').reduce((memo, item) => (memo += (pos = alphabet.indexOf(item)) != -1 ? alphabet[pos + 13] : item), '');
- };
- ==============================
- function add(n) {
- if (typeof add.summ === "number") {
- add.summ += n;
- } else {
- add.summ = n
- };
- return add;
- }
- Function.prototype.toString = function () {
- var res = this.summ || "No sum";
- delete this.summ;
- return res;
- }
- alert( add(1) ) // выведет '1'
- alert( add(1)(9) ) // выведет '10'
- alert( add(4)(4)(4) ) // выведет '12'
Advertisement
Add Comment
Please, Sign In to add comment