admin管理员组文章数量:1023838
I'm trying to write a script that prompts the user for a string containing numbers separated by spaces, and then alerts the minimum value, maximum value, and the sum.
Here is what I have so far:
var prompt = (“Enter a string containing numbers separated by spaces”);
I'm trying to write a script that prompts the user for a string containing numbers separated by spaces, and then alerts the minimum value, maximum value, and the sum.
Here is what I have so far:
var prompt = (“Enter a string containing numbers separated by spaces”);
Share
Improve this question
edited Oct 16, 2012 at 2:16
coderabbi
2,30116 silver badges18 bronze badges
asked Oct 16, 2012 at 1:58
tonkatonka
11 silver badge1 bronze badge
3
- What, specifically, is your question? – Terry Commented Oct 16, 2012 at 2:02
-
2
Careful with those curly quotes, you want to use regular quotes
""
. Writing code in a word processor is not a good idea, use a text editor. – elclanrs Commented Oct 16, 2012 at 2:05 - how do i alert the code to get the min value, max value, and sum? should i use the split operation? – tonka Commented Oct 16, 2012 at 2:07
3 Answers
Reset to default 2var input = prompt('Enter a string containing numbers separated by spaces').split(' ');
var min = Math.min.apply(Math, input);
var max = Math.max.apply(Math, input);
var sum = 0;
for(var i=0; i<input.length; i++){
var n = parseFloat(input[i], 10); // depending on expected input
sum += n ? n : 0; // add if number
}
alert('min: '+min+'\nmax: '+max+'\nsum: '+sum);
==> jsfiddle
Edit:
If you are not sure that only valid input is given you have to check the array manually instead of using Math.min
and Math.max
, wich could result in NaN
// Some variables we'll be using
var numbers = "", arr, min, max, sum = 0;
// Don't let them pass without valid input
while ( ! numbers.test(/^(\d+\s?)+$/) ) {
numbers = prompt( "Enter some numbers, separated by spaces." );
}
// Determine min, max, and the sum
arr = numbers.split(" ");
min = Math.min.apply(null, arr);
max = Math.max.apply(null, arr);
for ( var i = 0; i < arr.length; i++ ) {
sum += (+arr[i]);
}
// Output results into console
console.log( min, max, sum );
Fiddle: http://jsfiddle/GUcgj/
var input = prompt("Enter a string containing numbers separated by spaces");
var ary = input.split(" ");
This would load the input into the array ary
. However, you'd need to trim any white space and check for proper input before doing any further processing.
I'm trying to write a script that prompts the user for a string containing numbers separated by spaces, and then alerts the minimum value, maximum value, and the sum.
Here is what I have so far:
var prompt = (“Enter a string containing numbers separated by spaces”);
I'm trying to write a script that prompts the user for a string containing numbers separated by spaces, and then alerts the minimum value, maximum value, and the sum.
Here is what I have so far:
var prompt = (“Enter a string containing numbers separated by spaces”);
Share
Improve this question
edited Oct 16, 2012 at 2:16
coderabbi
2,30116 silver badges18 bronze badges
asked Oct 16, 2012 at 1:58
tonkatonka
11 silver badge1 bronze badge
3
- What, specifically, is your question? – Terry Commented Oct 16, 2012 at 2:02
-
2
Careful with those curly quotes, you want to use regular quotes
""
. Writing code in a word processor is not a good idea, use a text editor. – elclanrs Commented Oct 16, 2012 at 2:05 - how do i alert the code to get the min value, max value, and sum? should i use the split operation? – tonka Commented Oct 16, 2012 at 2:07
3 Answers
Reset to default 2var input = prompt('Enter a string containing numbers separated by spaces').split(' ');
var min = Math.min.apply(Math, input);
var max = Math.max.apply(Math, input);
var sum = 0;
for(var i=0; i<input.length; i++){
var n = parseFloat(input[i], 10); // depending on expected input
sum += n ? n : 0; // add if number
}
alert('min: '+min+'\nmax: '+max+'\nsum: '+sum);
==> jsfiddle
Edit:
If you are not sure that only valid input is given you have to check the array manually instead of using Math.min
and Math.max
, wich could result in NaN
// Some variables we'll be using
var numbers = "", arr, min, max, sum = 0;
// Don't let them pass without valid input
while ( ! numbers.test(/^(\d+\s?)+$/) ) {
numbers = prompt( "Enter some numbers, separated by spaces." );
}
// Determine min, max, and the sum
arr = numbers.split(" ");
min = Math.min.apply(null, arr);
max = Math.max.apply(null, arr);
for ( var i = 0; i < arr.length; i++ ) {
sum += (+arr[i]);
}
// Output results into console
console.log( min, max, sum );
Fiddle: http://jsfiddle/GUcgj/
var input = prompt("Enter a string containing numbers separated by spaces");
var ary = input.split(" ");
This would load the input into the array ary
. However, you'd need to trim any white space and check for proper input before doing any further processing.
本文标签: javascriptPrompt the user for a stringStack Overflow
版权声明:本文标题:javascript - Prompt the user for a string - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745548454a2155506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论