共计 5578 个字符,预计需要花费 14 分钟才能阅读完成。
???? 前言
大家好呀,我是毛小悠,能够叫我二毛,在家中排行老二,是一名前端开发工程师。
本系列文章旨在通过练习来进步 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";
}
}
???? 后序
本系列会定期更新的,题目会由浅到深的逐步提高。
求关注求点赞 ????~~????????????
能够关注我的公众号: 前端毛小悠 。欢送浏览
正文完
发表至: javascript
2021-01-30