简体   繁体   English

取消移位并推入 Javascript 中的循环,无限循环 javascript

[英]unshift and push inside a loop in Javascript, infinite loop javascript

I'm a beginner student and I'm try to get some information from "user" using the prompt() function, and then throw this information in an array.我是初学者,我尝试使用 prompt() function 从“用户”那里获取一些信息,然后将这些信息放入一个数组中。 I need to use the loop FOR and the WHILE to solve this problem.我需要使用循环 FOR 和 WHILE 来解决这个问题。

This is my code:这是我的代码:

let allEmployeess = Number(prompt("How many employees in the company?"));

let employee = new Array(allEmployeess);
let contador = 1;

for (let i = 0; i <= employee.length; i++) {
  let colaborador = {
    name: prompt("Employee name:"),
    salary: Number(prompt("What is the salary amount??"))
  }

  employee.unshift(colaborador);
}

This isn't working and I'm falling into an infinite loop.这不起作用,我陷入了无限循环。

I suppose this happens because the unshift(and push) method, return the new lenght of the array, and maybe I'm increasing the array leaving it always bigger than the counter.我想这是因为 unshift(and push) 方法返回数组的新长度,也许我正在增加数组,使它总是比计数器大。

What can I do to solve this?我能做些什么来解决这个问题?

The were a few minor mistakes in your code which I tried to address with comments:您的代码中有一些小错误,我试图通过评论来解决:

let allEmployeess = Number(prompt("How many employees in the company?"));

let employee = new Array(allEmployeess);
let contador = {}; // better to have an empty object instead of 1 for readablity

for (let i = 0; i < employee.length; i++) { // when you are iterating an array, it should go from 0 to length -1
  let colaborador = {
    name: prompt("Employee name:"),
    salary: Number(prompt("What is the salary amount??"))
  }

  employee[i] = colaborador; // instead of removing ith element, just update it with the new inputs
}

As you point out, you're continuously adding to the length of the array so employee.length keeps increasing, hence the infinite loop.正如您所指出的,您不断增加数组的长度,因此 employee.length 不断增加,因此无限循环。

It looks like what you want in your for loop conditional is i <= allEmployees rather than i <= employees.length as allEmployees is the total count of employees and will not change.看起来你在 for 循环条件中想要的是i <= allEmployees而不是i <= employees.length因为 allEmployees 是员工总数并且不会改变。

You are looping through an array that you keep adding elements to, hence the infinite loop.您正在遍历一个不断向其添加元素的数组,因此是无限循环。 Use the number of employees (scalar) in the for loop instead.请改用 for 循环中的员工人数(标量)。 Also, I created the colaborador object outside of the loop, so it only takes up one place in memory:另外,我在循环外创建了colaborador object,所以它在 memory 中只占一个位置:

 let allEmployeess = Number(prompt("How many employees in the company?")); let colaborador = {}; let employee = []; for (let i = 0; i < allEmployeess; i++) { colaborador["name"] = prompt("Employee name:"); colaborador["salary"] = Number(prompt("What is the salary amount??")); employee.unshift(colaborador); colaborador = {}; } console.log(employee);

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

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