🚀 前言
大家好呀,我是毛小悠,可以叫我二毛,在家中排行老二,是一名前端开发工程师。
本系列文章旨在通过练习来提高JavaScript的能力,一起愉快的做题吧。😀😀😀
以下每道题,二毛我都有尝试做一遍。建议限时训练,比如限定为半小时,如果半小时内想不出来,可以结合文章末尾的参考答案来思考。
可以在下方评论区留言或者加我的微信:code_maomao。期待你的到来。
求关注求点赞👍~~~😘😘😘
📖 题目1:用函数计算
这次我们要使用函数编写计算并获得结果。让我们看一些例子:
seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(dividedBy(two())); // must return 3
要求:
- 每个数字必须有一个函数,范围从0(“零”)到9(“ 9”)
- 以下每个数学运算必须具有一个函数:加,减,乘,除(
divided_by
在Ruby和Python中) - 每次计算仅由一个操作和两个数字组成
- 最外层的函数代表左操作数,最内层的函数代表右操作数
- 除法应为整体除法。例如,应返回2,而不是2.666666...
eight(dividedBy(three()));
习题代码
function zero() {}
function one() {}
function two() {}
function three() {}
function four() {}
function five() {}
function six() {}
function seven() {}
function eight() {}
function nine() {}
function plus() {}
function minus() {}
function times() {}
function dividedBy() {}
📖 题目2:RGB到十六进制转换
rgb功能不完整。把RGB十进制值变为十六进制表示形式。RGB的有效十进制转换0-255。任何超出该范围的值都必须四舍五入为最接近的有效值。注意:您的答案应始终为6个字符长,带有3个字符的#FFF速记在这里不起作用。
以下是预期输出值的示例:
rgb(255, 255, 255) // returns FFFFFF
rgb(255, 255, 300) // returns FFFFFF
rgb(0,0,0) // returns 000000
rgb(148, 0, 211) // returns 9400D3
习题代码:
rgb(r,g,b){
}
📖 题目3:谁喜欢呢?
您可能从Facebook和其他页面知道“喜欢”机制。
实现一个函数likes :: [String] -> String
likes [] -- must be "no one likes this"
likes ["Peter"] -- must be "Peter likes this"
likes ["Jacob", "Alex"] -- must be "Jacob and Alex like this"
likes ["Max", "John", "Mark"] -- must be "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] -- must be "Alex, Jacob and 2 others like this"
对于4个或更多的名称,数字and 2 others
习题代码
function likes(names) {
// TODO
}
答案
🍗 题目1的答案
参考答案1:
var n = function(digit) {
return function(op) {
return op ? op(digit) : digit;
}
};
var zero = n(0);
var one = n(1);
var two = n(2);
var three = n(3);
var four = n(4);
var five = n(5);
var six = n(6);
var seven = n(7);
var eight = n(8);
var nine = n(9);
function plus(r) { return function(l) { return l + r; }; }
function minus(r) { return function(l) { return l - r; }; }
function times(r) { return function(l) { return l * r; }; }
function dividedBy(r) { return function(l) { return l / r; }; }
参考答案2:
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; }; };
参考答案3:
['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 } }
参考答案4:
'zero one two three four five six seven eight nine'.split(' ').forEach(
(mth, num) => this[mth] = (f = val => val) => f(num)
)
let plus = (r) => (l) => l + r
let minus = (r) => (l) => l - r
let times = (r) => (l) => l * r
let dividedBy = (r) => (l) => l / r
参考答案5:
const
id = x => x,
number = x => (f = id) => f(x),
[zero, one, two, three, four, five, six, seven, eight, nine] =
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(number),
plus = x => y => y + x,
minus = x => y => y - x,
times = x => y => y * x,
dividedBy = x => y => y / x;
🍗 题目2的答案
参考答案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()
}
参考答案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();
}
参考答案3:
function rgb(r, g, b){
function toHex(a) {
if (a <= 0) return '00';
else if (a >= 255) return 'FF';
else return a.toString(16).toUpperCase();
}
return toHex(r) + toHex(g) + toHex(b);
}
参考答案4:
const rgb = (r, g, b) => toHex(r) + toHex(g) + toHex(b);
function toHex(numb) {
if (numb <= 0) return '00';
if (numb > 255) return 'FF';
return numb.toString(16).toUpperCase();
}
参考答案5:
let rgb = (r, g, b) => [r,g,b].map( (item) => item>=255 ? 'FF' : item<=0 ? '00' : item.toString(16).toUpperCase()).join('')
🍗 题目3的答案
参考答案1:
function likes(names) {
names = names || [];
switch(names.length){
case 0: return 'no one likes this'; break;
case 1: return names[0] + ' likes this'; break;
case 2: return names[0] + ' and ' + names[1] + ' like this'; break;
case 3: return names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this'; break;
default: return names[0] + ', ' + names[1] + ' and ' + (names.length - 2) + ' others like this';
}
}
参考答案2:
function likes(names) {
return {
0: 'no one likes this',
1: `${names[0]} likes this`,
2: `${names[0]} and ${names[1]} like this`,
3: `${names[0]}, ${names[1]} and ${names[2]} like this`,
4: `${names[0]}, ${names[1]} and ${names.length - 2} others like this`,
}[Math.min(4, names.length)]
}
参考答案3:
function likes (names) {
var templates = [
'no one likes this',
'{name} likes this',
'{name} and {name} like this',
'{name}, {name} and {name} like this',
'{name}, {name} and {n} others like this'
];
var idx = Math.min(names.length, 4);
return templates[idx].replace(/{name}|{n}/g, function (val) {
return val === '{name}' ? names.shift() : names.length;
});
}
参考答案4:
function likes(names) {
if(names.length === 0) return "no one likes this";
if(names.length === 1) return names[0] + " likes this";
if(names.length === 2) return names[0] + " and " + names[1] + " like this";
if(names.length === 3) return names[0] + ", " + names[1] + " and " + names[2] + " like this";
return names[0] + ", " + names[1] + " and " + (names.length - 2) + " others like this";
}
参考答案5:
function likes(names) {
switch(names.length){
case 0:
return "no one likes this";
case 1:
return names[0] + " likes this";
case 2:
return names[0] + " and " + names[1] + " like this";
case 3:
return names[0] + ", " + names[1] + " and " + names[2] + " like this";
default:
return names[0] + ", " + names[1] + " and " + (names.length-2) + " others like this";
}
}
🍁后序
本系列会定期更新的,题目会由浅到深的逐步提高。
求关注求点赞 👍~~🍭🍭🍭
可以关注我的公众号:前端毛小悠。欢迎阅读
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。