Chuyển số la mã(roman) sang số nguyên

function solution(roman){
   var conversion = {M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1};
   
   return roman.match(/CM|CD|XC|XL|IX|IV|\w/g).reduce((accum, roman) => accum + conversion[roman], 0);
}

This time we want to write calculations using functions and get the results. Let’s have a look at some examples:

seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(dividedBy(two())); // must return 3
  • There must be a function for each number from 0 (“zero”) to 9 (“nine”)
  • There must be a function for each of the following mathematical operations: plus, minus, times, dividedBy (divided_by in Ruby and Python)
  • Each calculation consist of exactly one operation and two numbers
  • The most outer function represents the left operand, the most inner function represents the right operand
  • Division should be integer division. For example, this should return 2, not 2.666666...: eight(dividedBy(three()));
function zero(func)   { return func ? func(0) : 0; };
function one(func)    { return func ? func(1) : 1; };
function two(func)    { return func ? func(2) : 2; };
function three(func)  { return func ? func(3) : 3; };
function four(func)   { return func ? func(4) : 4; };
function five(func)   { return func ? func(5) : 5; };
function six(func)    { return func ? func(6) : 6; };
function seven(func)  { return func ? func(7) : 7; };
function eight(func)  { return func ? func(8) : 8; };
function nine(func)   { return func ? func(9) : 9; };

function plus( b )      { return function( a ) { return a + b; }; };
function minus( b )     { return function( a ) { return a - b; }; };
function times( b )     { return function( a ) { return a * b; }; };
function dividedBy( b ) { return function( a ) { return a / b; }; };
['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
.forEach(function (name, n) {
  this[name] = function (f) { return f ? f(n) : n }
});

function plus(n)      { return function (a) { return a + n } }
function minus(n)     { return function (a) { return a - n } }
function times(n)     { return function (a) { return a * n } }
function dividedBy(n) { return function (a) { return a / n } }

 

function zero() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "0" + arguments[0] ) );
  }
  return 0;
}
function one() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "1" + arguments[0] ) );
  }
  return 1;
}
function two() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "2" + arguments[0] ) );
  }
  return 2;
}
function three() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "3" + arguments[0] ) );
  }
  return 3;
}
function four() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "4" + arguments[0] ) );
  }
  return 4;  
}
function five() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "5" + arguments[0] ) );
  }
  return 5;  
}
function six() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "6" + arguments[0] ) );
  }
  return 6;  
}
function seven() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "7" + arguments[0] ) );
  }
  return 7;  
}
function eight() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "8" + arguments[0] ) );
  }
  return 8;  
}
function nine() {
  if( arguments.length > 0 ){
    return Math.floor( eval( "9" + arguments[0] ) );
  }
  return 9;  
}

function plus() {
  if( arguments.length > 0 ){
    return "+" + arguments[0];
  }
  return `+`;  
}
function minus() {
  if( arguments.length > 0 ){
    return "-" + arguments[0];
  }
  return `-`;   
}
function times() {
  if( arguments.length > 0 ){
    return "*" + arguments[0];
  }
  return `*`;    
}
function dividedBy() {
  if( arguments.length > 0 ){
    return "/" + arguments[0];
  }
  return `/`;  
}

RGB To Hex Conversion

The rgb function is incomplete. Complete it so that passing in RGB decimal values will result in a hexadecimal representation being returned. Valid decimal values for RGB are 0 – 255. Any values that fall out of that range must 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
// my solution
function rgb(r, g, b){
  // complete this function
  var result = '';
  for( let i = 0; i < arguments.length; i++ ){
    if( arguments[i] <= 0 ){
      result+= '00';
    }else if( arguments[i] >= 255 ){
      result+= 'FF';
    }else if( arguments[i] > 0 && arguments[i] <= 15 ){
      result+= "0" + arguments[i].toString(16);
    }else{
      result+= arguments[i].toString(16);
    }
  }
  return result.toUpperCase();
}
// reference solutions
function rgb(r, g, b){
  // complete this function
  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 ).toUpperCase() ).slice(-2);
}
function rgb(r, g, b){
  return [r,g,b].map(function(x) {
    return ('0'+Math.max(0, Math.min(255, x)).toString(16)).slice(-2);
  }).join('').toUpperCase();
}

Simple Pig Latin

Move the first letter of each word to the end of it, then add “ay” to the end of the word. Leave punctuation marks untouched. ex:

pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !');     // elloHay orldway !
function pigIt(str) {
  return str.replace(/\w+/g, (w) => {
    return w.slice(1) + w[0] + 'ay';
  });
}

The Hashtag Generator #

The marketing team is spending way too much time typing in hashtags.
Let’s help them with our own Hashtag Generator!

Here’s the deal:

  • It must start with a hashtag (#).
  • All words must have their first letter capitalized.
  • If the final result is longer than 140 chars it must return false.
  • If the input or the result is an empty string it must return false.
" Hello there thanks for trying my Kata"  =>  "#HelloThereThanksForTryingMyKata"
"    Hello     World   "                  =>  "#HelloWorld"
""                                        =>  false
function generateHashtag (str) {
  if( !str || str.length > 140 ) return false;
  
  var result = "#" + str.trim().replace( /\w+/g, w => { return w[0].toUpperCase() + w.slice(1) } );
  return result.split(' ').join('');
}

 

 

Bài viết liên quan

post-no-image

Kỹ thuật debounce trong javascript – Trì hoãn nhập từ khóa trong ô input

post-no-image

Trang tổng hợp một số thư viện Javascript, Awesome JavaScript libraries

post-no-image

Những bài viết hay về lập trình javascript

post-no-image

Thư viện localForage ( API localStorage ) và ứng dụng