Return 最短和最长单词的长度差(使用for循环)
Return the difference between the lengths of the shortest and longest words (using for loop)
我想知道如何 return 最短和最长单词的长度之间的差异(使用 for 循环):
function findShort(s){
s = s.split(" ");
(for let i = 0; i < s.length; i++) {
******code goes here******
}
}
findShort("bitcoin take over the world maybe who knows perhaps");
findShort("turns out random test cases are easier than writing out basic ones");
});
有什么想法吗?如果有人也想尝试使用 map 或 reduce 之类的东西,我们也将不胜感激。
干杯!
解决方案
- 将数组映射到单词的长度。
- 然后对数组进行排序,它看起来像
[2, 3, 4, 5, 6, 7, 7]
- 然后构建最长和最短单词之间的差异。
这就是你的结果
function findShort(s){
s = s.split(" ");
let leng = s.map((x) => {return x.length})
leng.sort();
return leng[leng.length-1] - leng[0];
}
console.log(findShort("bitcoin take over the world maybe who knows perhaps"));
console.log(findShort("turns out random test cases are easier than writing out basic ones"));
将数组缩减为一对 short
和 long
值,以及 return 增量:
const findDelta = str => {
const [long, short] = str.split(/\s+/)
.reduce(([long, short], { length }) => [
Math.max(long, length), // take the longest length
Math.min(short, length) // take the shortest length
], [-Infinity, Infinity]) // init long with -Infinity and short with Infinity
return long - short
}
console.log(findDelta("bitcoin take over the world maybe who knows perhaps"))
console.log(findDelta("turns out random test cases are easier than writing out basic ones"))
不需要 for 循环。只需将句子映射到单词长度列表中,找到最长和最短的并减去:
function findShort(sentence) {
const lengths = sentence.split(/\s+/).map(word => word.length);
return Math.max(...lengths) - Math.min(...lengths);
}
console.log(findShort("bitcoin take over the world maybe who knows perhaps"));
console.log(findShort("turns out random test cases are easier than writing out basic ones"));
我想知道如何 return 最短和最长单词的长度之间的差异(使用 for 循环):
function findShort(s){
s = s.split(" ");
(for let i = 0; i < s.length; i++) {
******code goes here******
}
}
findShort("bitcoin take over the world maybe who knows perhaps");
findShort("turns out random test cases are easier than writing out basic ones");
});
有什么想法吗?如果有人也想尝试使用 map 或 reduce 之类的东西,我们也将不胜感激。
干杯!
解决方案
- 将数组映射到单词的长度。
- 然后对数组进行排序,它看起来像
[2, 3, 4, 5, 6, 7, 7]
- 然后构建最长和最短单词之间的差异。
这就是你的结果
function findShort(s){
s = s.split(" ");
let leng = s.map((x) => {return x.length})
leng.sort();
return leng[leng.length-1] - leng[0];
}
console.log(findShort("bitcoin take over the world maybe who knows perhaps"));
console.log(findShort("turns out random test cases are easier than writing out basic ones"));
将数组缩减为一对 short
和 long
值,以及 return 增量:
const findDelta = str => {
const [long, short] = str.split(/\s+/)
.reduce(([long, short], { length }) => [
Math.max(long, length), // take the longest length
Math.min(short, length) // take the shortest length
], [-Infinity, Infinity]) // init long with -Infinity and short with Infinity
return long - short
}
console.log(findDelta("bitcoin take over the world maybe who knows perhaps"))
console.log(findDelta("turns out random test cases are easier than writing out basic ones"))
不需要 for 循环。只需将句子映射到单词长度列表中,找到最长和最短的并减去:
function findShort(sentence) {
const lengths = sentence.split(/\s+/).map(word => word.length);
return Math.max(...lengths) - Math.min(...lengths);
}
console.log(findShort("bitcoin take over the world maybe who knows perhaps"));
console.log(findShort("turns out random test cases are easier than writing out basic ones"));