题目
A trick I learned in elementary school to determine whether or not a number was divisible by three is to add all of the integers in the number together and to divide the resulting sum by three. If there is no remainder from dividing the sum by three, then the original number is divisible by three as well.
Given a series of digits as a string, determine if the number represented by the string is divisible by three.
You can expect all test case arguments to be strings representing values greater than 0.
小学时学过一个技巧来判断一个数是否可以被3整除,就是把这个数中的所有整数相加,然后把所得的和除以3。如果和除以3没有余数,那么原始数也可以被3整除。
给定一系列数字作为字符串,确定字符串表示的数字是否可以被3整除。
所有测试用例参数都是表示大于0的值的字符串。
例子
"123" -> true
"8409" -> true
"100853" -> false
"33333333" -> true
"7" -> false
题解一
// 解法一:
function divisibleByThree(str){
return str.split('').reduce((sum,i)=> parseInt(sum)+parseInt(i)) % 3 == 0
}
题解二
// 解法二:
function divisibleByThree(str){
return [...str].reduce((s,d)=>+d+s,0)%3===0;
}