我正在尝试在javascript
中创建一个简单的函数,以接收数字提示输入。存储它。当所有输入数字的总和等于或大于50时,将退出提示,并显示console.log
以显示键入的数字和总和。
这是链接我的JS文件的HTML代码。
<!DOCTYPE html>
<html>
<head>
<title>Ex6 JS</title>
<script type="text/javascript" src="ex6.js"></script>
</head>
<body>
<h1>Exercise 6 JS</h1>
</body>
</html>
var my_array = [];
var sum=0;
var num_infinito = parseInt(prompt('Introduzca todos sus numeros'));
my_array.push(num_infinito);
if(my_array >= 50){
cantidadNumeros();
sumaNumeros();
}else{
obtenerMasNumeros();
}
function obtenerMasNumeros(){
var num_infinito = parseInt(prompt('Introduzca todos sus numeros'));
my_array.push(num_infinito);
for (var i = 0; i < my_array.length; i++) {
sum += my_array[i++];
if (sum>50) {
cantidadNumeros();
sumaNumeros();
} else if (sum<50){
var num_infinito = parseInt(prompt('Introduzca todos sus numeros'));
my_array.push(num_infinito);
} else {
cantidadNumeros();
sumaNumeros();
}
}
}
function cantidadNumeros(){
//var my_arrayIndex = my_array.length;
console.log('Has ingresado '+ String(my_array.length) + ' numeros');
}
function sumaNumeros(){
console.log('La suma de los numeros ingresados es ' + String(sum));
}
到目前为止,此代码的输出是:
提示要求我输入数字。如果数字> = 50,它将停止,但是如果数字<= 50(假设我输入40),它将进入功能。一旦进入函数,我将输入50,但是与其在第一个“ if”语句中停止,不如直接进入“ else if”语句。因此:系统提示我输入另一个数字40。
输入40后,输出为:
Has ingresado 3 numeros
La suma de los numeros ingresados es 80
因此,出于某种原因,第二个50被忽略,它直接跳到第三个输入数字(40),并将其添加到第一个输入数字。
第二
如果提示我键入,然后键入[1],然后继续键入2,然后依次是3和4,则该函数将在4处停止并返回具有4个数字(1、2、3、4)的数组但需要任何输出
第三
说,第一个输入是40,然后是10,然后是10。输出是:
total sum = 50 and my_array[3]
我添加了一些非常详尽的注释来解释流程。我还向您展示了在检查单个值(作用域并访问var)时应采取的措施。这样,您可以通过更改一个数字来更改阈值。使测试[[WAY更容易。自己尝试!
您与如何安排和触发代码有一些冲突。我添加了一些评论,希望能向您展示如何设置条件流。如果您对我可以帮助您的代码还有其他疑问,请告诉我。 :)var my_array = [];
// initial states are cleared on document load
var sum=0;
var thresh=50;
// This initially gets the obtener loop started by feeding it a number to start with
var num_infinito = parseInt(prompt('Introduzca todos sus numeros'));
my_array.push(num_infinito);
if(my_array >= thresh){
// There will only be one value on start, so we don't need to loop here. Sum is the only value, index[0]
sum = my_array[0];
// Show values and run final function
cantidadNumeros();
sumaNumeros();
}else{
// If value does not exceed the threshold, continue with running the function
obtenerMasNumeros();
}
function obtenerMasNumeros(){
// Grabbing another value
var num_infinito = parseInt(prompt('Introduzca todos sus numeros'));
// Add it to array
my_array.push(num_infinito);
// Re-init sum to go through the entire array
sum = 0;
var exceed = false; // We want to go through array no matter what
for (var i = 0; i < my_array.length; i++)
{
// Where we add the sum (first time is 0 + my_array[i])
sum += my_array[i];
if (sum >= thresh)
{
// If we exceed 50, we want to set the flag
exceed = true;
}
}
// We've iterated and gotten the sum. Now we check if it's exceeded the threshold or not
if ( exceed )
{
// Threshold is broken, run final sequence and return
cantidadNumeros();
sumaNumeros();
return;
// We return here to avoid the code below from running
}
obtenerMasNumeros();
}
function cantidadNumeros(){
console.log('Has ingresado '+ my_array.length + ' numeros');
}
function sumaNumeros(){
console.log('La suma de los numeros ingresados es ' + String(sum));
}