简体   繁体   English

如何在 javascript 中迭代一系列提示输入直到特定数字?

[英]How to iterate through a series of prompted inputs up to a specific number in javascript?

I'm trying to make a simple function in javascript that takes in a numerical prompt input.我正在尝试在 javascript 中制作一个简单的javascript ,它接受数字提示输入。 Stores it.存储它。 And once the Sum of all input number is equal or greater to 50, for the prompt to quit and for a console.log to show the amount of numbers typed in and the total sum.一旦所有输入数字的总和等于或大于 50,就会提示退出并让console.log显示输入的数字数量和总和。

This is the HTML code that links my JS file.这是链接我的 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));
}

The output for this code so far is:到目前为止,此代码的 output 是:

A prompt asking me to input a number.提示要求我输入数字。 If number >= 50 it stops, but if number <= 50 (lets say I input 40) it enters the function.如果数字 >= 50 它会停止,但如果数字 <= 50(假设我输入 40)它会进入 function。 Once it enters the function I will type 50, but instead of stopping in the first "if" statement it goes straight to the "else if" statemente.一旦它进入 function 我将输入 50,但不是在第一个“if”语句中停止,而是直接进入“else if”语句。 So: I am prompted to input another number, 40.所以:我被提示输入另一个数字,40。

After inputting 40 the output is:输入 40 后 output 为:

Has ingresado 3 numeros La suma de los numeros ingresados es 80 Has ingresado 3 numeros La suma de los numeros ingresados es 80

So, for some reason the second 50 is ignored and it skips straight to the third input number (40) and adds that to the first input number.因此,由于某种原因,第二个 50 被忽略,它直接跳到第三个输入数字 (40) 并将其添加到第一个输入数字。

SECONDLY其次

if I am prompted to type and I type [1], and then go on to type 2 and then 3 and then 4, the function stops at 4 and returns an array with 4 numbers (1, 2, 3, 4) but withought any output如果提示我键入并键入 [1],然后 go 键入 2,然后键入 3,然后键入 4,则 function 在 4 处停止并返回一个包含 4 个数字(1、2、3、4)的数组,但没有任何 output

THIRDLY第三次

Say, 40 is the first input, then, 10 then 10. the output is:假设第一个输入是 40,然后是 10,然后是 10。 output 是:

total sum = 50 and my_array[3]

I've added some pretty thorough comments to explain the flow.我添加了一些非常详尽的评论来解释流程。 I've also shown you what you should do when you're checking against a single value (scope it and access the var).我还向您展示了在检查单个值时应该做什么(范围并访问 var)。 That way, you can change the threshold by changing one number.这样,您可以通过更改一个数字来更改阈值。 Makes testing WAY easier.使测试容易。 Try for yourself!自己试试吧!

You had some conflicts with how you were arranging and firing your code.您与如何安排和触发代码有一些冲突。 I've added some comments to hopefully show you how to set up conditional flow.我添加了一些评论,希望能向您展示如何设置条件流。 Let me know if you have any more questions about the code that I can help you with.如果您对我可以帮助您的代码有任何疑问,请告诉我。 :) :)

 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)); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM