1.用字符串代表數(shù)字來進行算術(shù)操作
描述:
// 比如
seven( times( five() ) ); // 相當(dāng)于 7 * 5 = 35
four( plus( nine() ) ); // 4 + 9 = 13
eight( minus( three() ) ); // 8 - 3 = 5
six( dividedBy( two() ) ); // 6 / 3 = 2
看到這個第一眼就有點蒙逼付材,這肯定要使用高階函數(shù)來完成,但如何實現(xiàn)渣刷,內(nèi)心比較矛盾苗桂,最后看了一下別人的算法:
算法1:
["zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"].forEach(function (name, n) {
this[name] = function(f) {return f ? f(n) : n}
});
var plus = n =>
a =>
a + n;
var minus = n =>
a =>
a - n;
var times = n =>
a =>
a * n;
var minus = n =>
a =>
a / n;
解析:
首先forEach函數(shù)利用0-9對應(yīng)的索引返回一個函數(shù),
如果這個函數(shù)參數(shù)存在則調(diào)用f(n), 如果不存在則直接
等于索引值趴泌,即0對應(yīng)的是0逆屡,9對應(yīng)的是9;
然后就是加減乘除各自也是一個高階函數(shù)踱讨。
算法2:與上面類似
const numbers = 'zero one two three four five six seven eight nine'.split(' ')
const number = num => {
return operator => {
if (operator) return operator(num)
return num
}
}
const operator = op => {
return x => {
return y => {
return op(y, x)
}
}
}
// Numbers
numbers.forEach((name, index) =>
GLOBAL[name] = number(index)
)
// Operators
const plus = operator((x, y) => x + y)
const minus = operator((x, y) => x - y)
const times = operator((x, y) => x * y)
const dividedBy = operator((x, y) => x / y)
2.算出一個字符串中重復(fù)的字符個數(shù)(忽略大小寫)
描述:
"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabbcdeB" -> 2 # 'a' and 'b'
"indivisibility" -> 1 # 'i'
"Indivisibilities" -> 2 # 'i' and 's'
我的算法:
function duplicateCount(text) {
var result = [];
var finalResult = [];
// 將text中重復(fù)的字符添加到result中
text.toLowerCase().split("")
.filter((v, idx, arr) =>
if (arr.indexOf(v) !== arr.lastIndexOf(v)) {
result.push(v);
}
)
// 將result中重復(fù)的字符去掉
finalResult = result.filter((v, idx, arr) =>
arr.indexOf(v) === idx
);
return finalResult.length;
}
我的算法雖然可行,但是比較啰嗦砍的。別人的算法痹筛,利用正則:
function duplicateCount(text) {
return (
text
.toLowerCase().split("").sort().join("")
.match(/([^])\1+/g || []).length;
)
}
// 先講字符串全變?yōu)樾懭缓笞優(yōu)閿?shù)組,并且排序
// 然后再將字符按照從小到大組合成字符串
// 正則
// ([^]): 表示任意字符
// (..)\1: 表示前面()的組
// (..)\1+: 表示連著出現(xiàn)2次以及2次以上的
3.將字符串交替的變換大小寫
描述:
toWeirdCase( "String" );//=> returns "StRiNg"
toWeirdCase( "Weird string case" );
//=> returns "WeIrD StRiNg CaSe"
做了半天沒做出來廓鞠,當(dāng)需要多層嵌套時對數(shù)組操作時帚稠, 最好不要用箭頭函數(shù)
別人的:
function toWeirdCase(string) {
return string.split(/\s+/).map(function(word) {
return word.split("").map(function(letter, i) {
return (i % 2 === 0) ? letter.toUpperCase() : letter.toLowerCase()
// 或者
// return letter[(!(i%2))?"toUpperCase":"toLowerCase"]()
// 這種表示方法利用對象的特性
}).join("");
}).join(" ");
}
本質(zhì)上是高階函數(shù)的運用, 需要注意的是床佳, 當(dāng)我們使用map之后滋早,如果需要對內(nèi)部item,再進行一次map, 這個時候需要返回一個函數(shù)砌们, 在返回的函數(shù)中對map進行操作杆麸。
解析:
string.split(/\s+/)
//1. 將字符串出去空格變?yōu)閿?shù)組
// "hello world this" => ["hello", "world", "this"]
string.split(/s+/).map(function(word) {
// ...
})
// 2.對每個單詞進行操作
// 3.這時候需要對每個單詞再一次進行map操作
return word.split(""){}
// 拆成數(shù)組
// ["h", "e", "l", "l", "o"]
// 內(nèi)部邏輯處理之后合并成一個單詞返回
// ["HeLlO", "WoRlD", "ThIs"]