<!DOCTYPE HTML>
<html>
<body>
</body>
<Script>
"use strict";
function sumInput(){
let nums = [];
while(true){
let val = prompt("A number plz?", 0);
//should we cancel?
//valu should stay as String during the input
//to distinguish an empty String
if( val === "" || !isFinite(val) || val === null ) {
alert (val);
break;
}
//val === "" for preventing an empty input
//val === null for when the cancel button was pressed in prompt window
// !isFinite(val) I dont know....
//isFinite returns true when the val is +infinity, -infinity, or NaN
//isFinite is to catch an NaN which occurs from empty input
nums.push(+val);//
}
let sum = 0;
for (let num of nums){
sum += num;
}
return sum;
}
/*
my trial was...
let sum;
let val;
while(true){
let val = +prompt("Value?", );
if(typeof val != "number"
|| val == "cancel"
|| val.length == 0) {
break;
}
sum = sum + val;
alert(sum);
}
*/
alert(sumInput());
</Script>
</html>
<html>
<body>
</body>
<Script>
"use strict";
function sumInput(){
let nums = [];
while(true){
let val = prompt("A number plz?", 0);
//should we cancel?
//valu should stay as String during the input
//to distinguish an empty String
if( val === "" || !isFinite(val) || val === null ) {
alert (val);
break;
}
//val === "" for preventing an empty input
//val === null for when the cancel button was pressed in prompt window
// !isFinite(val) I dont know....
//isFinite returns true when the val is +infinity, -infinity, or NaN
//isFinite is to catch an NaN which occurs from empty input
nums.push(+val);//
}
let sum = 0;
for (let num of nums){
sum += num;
}
return sum;
}
/*
my trial was...
let sum;
let val;
while(true){
let val = +prompt("Value?", );
if(typeof val != "number"
|| val == "cancel"
|| val.length == 0) {
break;
}
sum = sum + val;
alert(sum);
}
*/
alert(sumInput());
</Script>
</html>
댓글
댓글 쓰기