问题描述:

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.

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

我的思路:

这道题做法很简单,主要就是通过toString(n)将10进制转换成16进制(注意,n的取值范围是2~36)。但是自己的写法就比较直接,很是繁琐,看到大神们的写法真是简洁。尤其是slice、map用法,拍案叫绝。

我的答案:

function rgb(r, g, b){
// complete this function
r=a(r);g=a(g);b=a(b);
r=r.toString(16).toUpperCase();
g=g.toString(16).toUpperCase();
b=b.toString(16).toUpperCase();
var c=r.concat(g).concat(b);
if(c=="000"){return "000000";}
else{return c;}
}
function a(n){
if(n>255){return n=255;}
if(n<0){return n=0;}
if(n>=0 && n<=255){return n;}
}

优秀答案:

(1)

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() //slice(-2)作用若为0,则返回00;若为255,则返回ff
}

(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); //将<0或>255的分别置为0或255.
}).join('').toUpperCase();
}

哈哈哈!

codewars--js--RGB To Hex Conversion的更多相关文章

  1. js中RGB转hex

    科普下颜色代码 RGB格式:220,20,60(三个数字组成) HEX格式:#DC143C 代码如下 var rgb = '220,20,60'; alert(rgb2hex(rgb)); //将RG ...

  2. [CodeWars][JS]实现链式加法

    在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...

  3. [CodeWars][JS]实现大整数加法

    问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...

  4. [CodeWars][JS]如何判断给定的数字是否整数

    问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...

  5. 763. Hex Conversion [LintCode naive]

    Description Given a decimal number n and an integer k, Convert decimal number n to base-k. 1.0<=n ...

  6. [C++] Variable/Hex conversion

    程序编译链接原理预处理:.c -> .i gcc -E hello.c -o hello.i 编译:.i / .c -> .sgcc -S hello.i -o hello.s 汇编:.s ...

  7. 763 Hex Conversion

    原题网址:http://www.lintcode.com/zh-cn/problem/hex-conversion/ Given a decimal number n and an integer k ...

  8. JS 处理十六进制颜色渐变算法-输入颜色,输出渐变rgb数组

    html颜色有几种表示方式: 英文单词颜色值:background-color:Blue:十六进制颜色值:background-color:#FFFFFF:  RGB颜色值三元数字:backgroun ...

  9. 数据可视化(1)--Chart.js

    Chart.js是一个HTML5图表库,使用canvas元素来展示各式各样的客户端图表,支持折线图.柱形图.雷达图.饼图.环形图等.在每种图表中,还包含了大量的自定义选项,包括动画展示形式. Char ...

随机推荐

  1. MySql查看修改l时区

    # 查看时区 show variables like '%time_zone%'; # 设置全局 set global time_zone='+8:00';  # 设置当前会话 set time_zo ...

  2. 1.HelloWorld 仪式感

    HelloWorld: 1.随便新建一个文件夹,存放代码. 2.新建一个java文件 文件后缀改为 .java Hello.java 系统可能没显示文件后缀名,我们需要手动打开 3.编写代码 publ ...

  3. [apue] 使用 Ctrl+S停止输出而不用挂起前台进程

    之前一直知道使用 Ctrl+Z 挂起前台进程来阻止进程运行,之后可以再通过 shell 的作业控制 (jobs / fg N) 来将后台进程切换为前台,从而继续运行. 最近学到一种新的方法,对于不停有 ...

  4. 【Docker】 windows10 docker 使用

    原文地址:https://www.aliyun.com/jiaocheng/872144.html 摘要:docker之前在Windows和MacOS上的版本都是通过Linux虚拟机的形式,而现在Do ...

  5. 你可能不知道的 Python 技巧

    英文 | Python Tips and Trick, You Haven't Already Seen 原作 | Martin Heinz (https://martinheinz.dev) 译者 ...

  6. Mysql梳理-关于索引/引擎与锁

    前言 最近突发新型肺炎,本来只有七天的春节假期也因为各种封锁延长到了正月十五,在家实在闲的蛋疼便重新研究了一下Mysql数据库的相关知识,特此总结梳理一下.本文主要围绕以下几点进行: 1.Mysql的 ...

  7. 使用doxygen

    Getting started with Doxygen 可执行文件doxygen是解析源文件并生成文档的主程序. 另外, 也可以使用可执行文件doxywizard, 是用于编辑配置文件, 以及在图形 ...

  8. selenium,统计某分支下有多少个同类子分支的方法(用于循环获取同类型子分支属性值)

    利用selenium自动化统计微博阅读数 查看微博阅读数的元素路径 微博列表中第一条微博的元素路径“//*[@id="Pl_Official_MyProfileFeed__20"] ...

  9. python3读取excel文档数据

    实现场景: 1.读取Excel表数据 2.把数据作为参数传给后面的函数 3.后面的函数循环读取参数执行操作 本案例Excel内容为下图,becks为表名 先贴代码 import xlrd #读取exc ...

  10. 清晰架构(Clean Architecture)的Go微服务: 编码风格

    编码风格在编程中是一个相对乏味的主题,但是合适的编码风格对一个有效的程序员是至关重要的. 它有三个组成部分: 程序结构 ( application layout) 编码规则或风格 命名约定 我已经在清 ...