文章目录
codewars-js练习
2021/3/28
github 地址
【1】<8kyu>【Remove exclamation marks】
Write function RemoveExclamationMarks which removes all exclamation marks from a given string.
example:
"Hello World!"// "Hello World"
solution
<script type="text/javascript">
function removeExclamationMarks(s) {
return s.replace(/[!]/g,'')
}
// 验证
console.log(removeExclamationMarks("Hello World!"));// "Hello World"
</script>
【2】<8kyu>【Convert number to reversed array of digits】
Given a random non-negative number, you have to return the digits of this number within an array in reverse order.
example:
348597 => [7,9,5,8,4,3]
solution
<script type="text/javascript">
function digitize(n) {
return [...`${n}`].map(el => parseInt(el)).reverse()
}
// 验证
console.log(digitize(348597));//[7,9,5,8,4,3]
</script>
知识点
js将数字转换为数字数组:
const converToArray = number => [...`${number}`].map(el => parseInt(el))
// 事例
converToArray(5678) // [5, 6, 7, 8]
converToArray(12345678) // [1, 2, 3, 4, 5, 6, 7, 8]
这篇文章分享了两道Codewars上的JavaScript练习题:1. RemoveExclamationMarks 函数去除字符串中的感叹号,2. ConvertNumberToReversedArrayOfDigits 将数字转换为逆序的数字数组。通过实例展示了如何用JavaScript实现这些功能。
223

被折叠的 条评论
为什么被折叠?



