我坚持这个练习,最后我告诉你我的输出是什么,但在练习描述之前,请提前感谢!
描述:
它接收一个带有数字和字母的数组,并返回它的美化数字。信件保持不变 美化过程是通过将所有数字加在一起将数字减少为单个数字:
123 = 6 because 1+2+3 = 6
9 = 9
9956 = 2 because 9+9+5+6 = 29 -> 2+9 = 11 -> 1+1 = 2
793 = 1 because 7+9+3 = 19 -> 1+9 = 10 -> 1+0 = 1
Example: beautifyNumbers([23,59, 4,'A','b']) returns [5, 5, 4, 'A', 'b']
我的代码:
function beautifyNumbers(array) {
var newArray = [];
array.forEach(function(element) {
// Checks if character is a letter and not a number
if (typeof element == "number") {
var sNumber = element.toString();
for (var i = 0, len = sNumber.length; i < len; i += 1) {
newArray.push(+sNumber.charAt(i));
// The "+" sign converts a String variable to a Number, if possible: +'21.2' equals Number(21.2).
// If the conversion fails, it return NaN.
// El método charAt() de String devuelve el carácter especificado de una cadena:
// var name="Brave new world"; name.charAt(0) => 'B'
}
console.log(newArray);;
} else {
// pushes numbers to the array without making
// any change to them
newArray.push(element);
}
});
// returns the array
return newArray;
}
beautifyNumbers([23, 59, 4, 'A', 'b'])
&#13;
我收到的输出是=&gt; [2, 3, 5, 9, 4, "A", "b"]
这是&#34;之前的&#34;在做总结之前还是我做错了什么?
答案 0 :(得分:7)
嗨,你可以像我在评论中提到的那样尝试。
function beautifyNumbers(array) {
var newArray = [];
array.forEach(function(element) {
// Checks if character is a letter and not a number
if (typeof element == "number") {
if(element %9 == 0 && element != 0)
newArray.push(9);
else
newArray.push(element%9);
} else {
newArray.push(element);
}
});
return newArray;
}
console.log(beautifyNumbers([1231, 0, 18, 27, 12354, 59, 4, 'A', 'b']))
&#13;
编辑: 谢谢@georg的建议。
答案 1 :(得分:3)
我已对您的代码进行了更改。请找到下面的代码,根据您的需要提供结果。
function beautifyNumbers(array) {
var newArray = [];
array.forEach(function (element) {
// Checks if character is a letter and not a number
if (typeof element == "number") {
var sNumber = element.toString();
var beutifySum = 0;
for (var i = 0, len = sNumber.length; i < len; i += 1) {
beutifySum += +sNumber.charAt(i);
}
beutifySum = beutifySum % 9 === 0 ? 9 : beutifySum % 9;
newArray.push(beutifySum);
} else {
// pushes numbers to the array without making
// any change to them
newArray.push(element);
}
});
console.log(newArray);
// returns the array
return newArray;
}
beautifyNumbers([23, 59, 4, 'A', 'b'])
答案 2 :(得分:2)
您可以使用recursion
+ array.reduce
function sumOfDigits(num) {
// Check if value is number or alphanumeric
if (!isNaN(num)) {
// Convert number and split to get individual values.
// Loop over values and add them
var sum = num.toString().split("").reduce(function(p, c) {
// +p is a shorthand for parseInt(p)
return +p + +c;
});
// check if number is greater than 10. If yes, repeat the process
if (sum >= 10) sum = sumOfDigits(sum);
return sum;
}
// if value is not number, return value
else return num;
}
var data = [123, 4, 567, 'a', "abc", 0];
data.forEach(function(n) {
console.log(sumOfDigits(n))
})
答案 3 :(得分:1)
试试这样:
function beautifyNumbers(array) {
var newArray = [];
array.forEach(function(element) {
// Checks if character is a letter and not a number
if (typeof element == "number") {
newArray.push(beautifyElement(element));
} else {
// pushes numbers to the array without making
// any change to them
newArray.push(element);
}
});
// returns the array
return newArray;
}
function beautifyElement(element){
var sNumber = element.toString();
var sum = 0;
for (var i = 0, len = sNumber.length; i < len; i += 1) {
sum = sum + +sNumber[i];
}
if(sum>9){
return beautifyElement(sum);
}else{
return sum;
}
}
beautifyNumbers([23, 59, 4, 'A', 'b'])
答案 4 :(得分:0)
在您的示例中,您应该使用以下内容更改部件:
发件人:强>
for (var i = 0, len = sNumber.length; i < len; i += 1) {
newArray.push(+sNumber.charAt(i));
}
console.log(newArray);;
以强>
var mySum = 0;
for (var i = 0, len = sNumber.length; i < len; i += 1) {
mySum += sNumber.charAt(i);
}
newArray.push(mySum);
console.log(newArray);
答案 5 :(得分:0)
function bsum(v) {
var res = (v+"").split('').reduce(function(a, b) {return (a | 0) + (b | 0);});
if(res | 0 > 10) return bsum(res) ;
else return res;
}
function beautifyNumbers(array) {
return array.map(function(v, i) {
if (typeof v == 'number') {
return bsum(v) | 0;
}
else return v;
});
}
答案 6 :(得分:0)
纯函数没有任何字符串操作,这将是我的解决方案;
function funnySum(n){
var d = Math.floor(Math.log10(n) +1), // get number of digits
sum = Array(d).fill() // prepare the array and place digits
.map((_,i) => Math.floor(n % Math.pow(10,d-i) / Math.pow(10,d-1-i)))
.reduce((p,c) => p+c); // get sum of array items
return sum > 9 ? funnySum(sum) : sum; // if still two digits continue...
}
console.log(funnySum(9956));
&#13;