我创建了一个程序,用于比较电话号码的总和数字并返回最大值。事情是我的代码很长,我知道使用循环运行数字可以使它更简单。我是编程的新手,当我把它放在循环中时,它会说"语法错误"但是,我并不确切知道我的语法错误是什么。我的最终目标是创建一个功能,询问用户"您要比较多少数字"然后使用问题的用户答案循环遍历程序多次,每次循环通过它比较电话号码是否大于当前最大值,如果是这样,用新号码替换一个称为最大号码的变量然后它不断地经历多次,最终返回最大的电话号码。 我还没有...所以我现在想要做的就是弄清楚如何让循环本身接受用户提示输入电话号码并运行添加它们。之后我会担心如何保存数字并进行比较。这就是我所拥有的:
var numbers = prompt("How many phone numbers would you like to
compare?");
For (i=0; i=numbers; i++) {
var PhoneNumber[i]= prompt("Give me a 10 digit phone number separated by dashes for example 234-234-2342");
var num1 = PhoneNumber.substring(0, 1);
var num2= PhoneNumber.substring(1, 2);
var num3= PhoneNumber.substring(2, 3);
var num4= PhoneNumber.substring(4, 5);
var num5= PhoneNumber.substring(5, 6);
var num6= PhoneNumber.substring(6, 7);
var num7= PhoneNumber.substring(8, 9);
var num8= PhoneNumber.substring(9, 10);
var num9= PhoneNumber.substring(10, 11);
var num10= PhoneNumber.substring(11, 12);
PhoneNumberTotal[i] = (+num1 + +num2 + +num3 + +num4 + +num5 + +num6 + +num7 + +num8 + +num9 + +num10);
console.log(PhoneNumberTotal[i];
};
这只是为了得到电话号码然后我会担心比较它们但这说错误,我真的不知道我在做什么所以我确定它是凌乱的。请帮忙!
答案 0 :(得分:0)
这是一个使用一些非常简单的Javascript功能的例子。
var numbers = prompt("How many phone numbers would you like to compare?");
// Use < here. Using = would give 1 too many numbers. For loops are lowercase.
for (var i=0; i<numbers; i++) {
var PhoneNumber[i] = prompt("Give me a 10 digit phone number separated by dashes for example 234-234-2342");
// Loop through the digits
for (var j=0; j<number.length; j++) {
// Skip dashes.
if ( j == 3 || j == 7 ) continue;
// Add digit to total, parsed as number.
PhoneNumberTotal[i] += (+number[j]);
}
console.log(PhoneNumberTotal[i]); // Needs closing parenth
};
对于更短的实现,您可以使用以下内容:
// replace: remove dashes
// split: turn string into array of chars
// reduce: loop and add each digit to sum, then return
var numbers = prompt("How many phone numbers would you like to compare?");
for (var i=0; i<numbers; i++) {
var PhoneNumber[i] = prompt("Give me a 10 digit phone number separated by dashes for example 234-234-2342");
PhoneNumberTotal[i] = PhoneNumber[i]
.replace(/-/g,"")
.split("")
.reduce(function(sum, digit) { return sum + (+digit); }, 0);
console.log(PhoneNumberTotal[i]);
};
答案 1 :(得分:0)
这是一个函数,它将对字符串中的所有数字求和:
function sumNumber(numStr) {
return numStr.match(/\d/g).reduce(function(a, b) {
return Number(a) + Number(b);
});
}
现在,您需要做的就是比较数字。
var numbers = prompt("How many phone numbers would you like to compare?"),
biggestPhone = { total : '0'},
numStr, sum;
for(var i = 0; i < numbers; i++){
numStr = prompt("Give me a 10 digit phone number separated by dashes for example 234-234-2342");
sum = sumNumber(numStr);
if(sum > biggestPhone.total) biggestPhone = { total : sum, number : numStr };
}
alert('Biggest number is ' + biggestPhone.number + ' which totals to: ' + biggestPhone.total);
答案 2 :(得分:0)
var numbers = prompt("How many phone numbers would you like to compare?");
var PhoneNumber=[];
var PhoneNumberTotal=[];
var maxSum=0,maxIndex;
//For is not right, it should be for
// i is not declared
// i<numbers , each time this condition is checked
for(var i=0; i<numbers; i++)
{
PhoneNumber[i]= prompt("Give me a 10 digit phone number separated by dashes for example 234-234-2342");
//remove dashes from the number and store in tempVar
// "/-/g" is regular expression which says identify all dashes in any thing
// read something about it
var tempVar=PhoneNumber[i].replace(/-/g,'');
PhoneNumberTotal[i]=0;
for(var j=0;j<tempVar.length;j++)
{
var jthCharOfTempVar=tempVar.charAt(j);
//convert string to integer and sum to PhoneNumberTotal[i]
PhoneNumberTotal[i]= PhoneNumberTotal[i]+parseInt(jthCharOfTempVar);
}
if(PhoneNumberTotal[i]>maxSum)
{
maxSum=PhoneNumberTotal[i];
maxIndex=i;
}
}
console.log("Greatest Number",PhoneNumber[maxIndex]);
console.log("SumOf digits",PhoneNumberTotal[i]);
答案 3 :(得分:0)
使用所有精彩的建议,但不想偏离我的原始代码,我能够解决这个问题。任何其他人都和我一样在同一条船上我发布我的解决方案,这个解决方案的目的是我正在制作代码,即使它不是每个人都建议的,如果它不是最好的&#39;寻找代码。
var numbers = prompt("How many phone numbers would you like to
compare?");
var PhoneNumber=[];
var PhoneNumberTotal=[];
var maxSum=0,maxIndex;
for(var i=0; i<numbers; i++) {
PhoneNumber[i]= prompt("Give me a 10 digit phone number separated by
dashes for example 234-234-2342");
var num1 = PhoneNumber[i].substring(0, 1);
var num2= PhoneNumber[i].substring(1, 2);
var num3= PhoneNumber[i].substring(2, 3);
var num4= PhoneNumber[i].substring(4, 5);
var num5= PhoneNumber[i].substring(5, 6);
var num6= PhoneNumber[i].substring(6, 7);
var num7= PhoneNumber[i].substring(8, 9);
var num8= PhoneNumber[i].substring(9, 10);
var num9= PhoneNumber[i].substring(10, 11);
var num10= PhoneNumber[i].substring(11, 12);
PhoneNumberTotal= +num1 + +num2 + +num3 + +num4 + +num5 + +num6 + +num7
+ +num8 + +num9 + +num10;
if(PhoneNumberTotal>maxSum)
{
maxSum=PhoneNumberTotal;
maxIndex=i;
}
};
console.log("Greatest Number",PhoneNumber[maxIndex]);
console.log("SumOf digits",PhoneNumberTotal);