如何遍历一系列提示输入直到 javascript 中的特定数字?
How to iterate through a series of prompted inputs up to a specific number in javascript?
我正在尝试在 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));
}
到目前为止,这段代码的输出是:
要求我输入数字的提示。如果 number >= 50 它停止,但如果 number <= 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 处停止,returns 一个包含 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));
}
我正在尝试在 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));
}
到目前为止,这段代码的输出是:
要求我输入数字的提示。如果 number >= 50 它停止,但如果 number <= 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 处停止,returns 一个包含 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));
}