codewars练习(javascript)-2021/3/28

这篇文章分享了两道Codewars上的JavaScript练习题:1. RemoveExclamationMarks 函数去除字符串中的感叹号,2. ConvertNumberToReversedArrayOfDigits 将数字转换为逆序的数字数组。通过实例展示了如何用JavaScript实现这些功能。

codewars-js练习

2021/3/28

github 地址

my 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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值