简体   繁体   English

循环学习。 无法弄清楚我的代码中缺少什么。

[英]Learning While Loops. Can't figure out what I am missing in my code.

Write a function called indexToString. 编写一个名为indexToString的函数。 This function should: 1. Take an array as an argument 2. Create and return a new array of all passed array elements as strings 3. Each strings should be formatted like so: “[index] is [element at index]” This is the error I am getting: returns an array of passed-in array elements as strings with element index specified expected undefined to deeply equal [ '0 is 1', '1 is 2', '2 is 3' ] 该函数应:1.以数组作为参数2.创建并返回所有传递的数组元素作为字符串的新数组3.每个字符串的格式应如下:“ [[index] is [element at index]”。我得到的错误:将传入的数组元素的数组作为具有指定元素索引的字符串返回,期望未定义为深度相等['0为1','1为2','2为3']

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

var indexToString = function(array){
  index = 0;
  elementAtIndex = 0;
  var i = 0;
  while(i < array.length){
    console.log(index + " is " + elementAtIndex);

    i++
  }
  return array[i];
};

Two Three errors. 二三 错误。

Firstly, the while loop will exit when i is no longer less than array.length ; 首先,当i不再小于array.length时,while循环将退出。 the first such number is array.length . 第一个这样的数字是array.length This means, at the end of the loop, array[i] is array[array.length] , which is just outside the array, thus undefined . 这意味着,在循环结束时, array[i]array[array.length] ,它刚好位于数组外部,因此undefined

Secondly, you are supposed to return an array of strings, as told by your test failure message; 其次,应该按照测试失败消息的指示返回一个字符串数组; not print them to the console. 不要将它们打印到控制台。

EDIT: Thirdly, what Spencer said. 编辑:第三,斯宾塞说了什么。 :) Use i instead of index , and array[i] instead of elementAtIndex . :)使用i代替index ,并使用array[i]代替elementAtIndex

You want to start with an empty array outside the loop, and push each string you construct into it; 您想从循环外部的空数组开始,并将构造的每个字符串压入其中; then return that array after the loop. 然后在循环后返回该数组。

Or you can do it with "new" JavaScript: 或者,您也可以使用“新” JavaScript:

var indexToString = array => array.map((e, i) => `${i} is ${e}`)

You should reflect a bit more on your code, it is quite nonsense so far. 您应该在代码上多花点功夫,到目前为止,这完全是胡说八道。

Let's decompose the question to identify what should be done: 让我们分解问题以确定应该做什么:

Write a function called indexToString . 编写一个名为indexToString的函数。 This function should: 该功能应:

  1. Take an array as an argument 数组作为参数

  2. Create and return a new array of all passed array elements as strings 创建并返回所有传递的数组元素的新数组作为字符串

  3. Each strings should be formatted like so: “[index] is [element at index]” 每个字符串的格式应如下: “ [索引]是[索引元素]”

So: 所以:

  • you create a function called indexToString which body contains the code, as you did. 您将创建一个名为indexToString的函数,该函数包含代码,就像您所做的那样。

  • In the initialization part before your while, you should create a new empty array that is going to be filled, and declare an integer called index for example initialized at 0 that is going to be used to loop through the original array. 在此之前的初始化部分 ,您应该创建一个将要填充的新的空数组 ,并声明一个名为index整数 ,例如初始化为0的整数 ,该整数将用于循环遍历原始数组。 (Tip: var new_array = []; to create, and use new_array.push( elem ); to append an element) (提示:var new_array = [];创建并使用new_array.push( elem );附加元素)

  • in your while, you should push at the end of your new array a string built as follow: "["+index+"] is ["+array[index]+"]" AND you should increment your index. 在这期间,您应该在新数组的末尾推送一个按如下方式构建的字符串:“ [” + index +“]是[” + array [index] +“]”,并且应该增加索引。 You loop while(index < array.length) . 您循环while(index < array.length)

At the end, you can return the new array ! 最后,您可以返回新数组

Good luck with learning programming! 祝您学习编程好运!

如果arraylenth为10,则函数将返回array [10]。这是数组绑定的。

When you enter the last loop, the i becomes equal to array.length , so array[i] is undefined after this. 当您进入最后一个循环时, i等于array.length ,因此此后未定义array[i] But probably this is not what you wanted. 但这可能不是您想要的。 you want to return the full array. 您想返回完整的数组。

 var indexToString = function(array){ var new_array = []; var i = 0; while(i < array.length){ new_array[i] = i + " is " + array[i]; i++; } return new_array; }; console.log(indexToString([1,2,3])); 

暂无
暂无

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

相关问题 学习循环,无法弄清楚这个是如何工作的 - Learning loops, can't figure out how this one works 我无法弄清楚我错过了什么,除了一个选项,代码100%工作 - I can't figure out what I'm missing, code works 100% except for one option 在 API 的帮助下创建网页时,无法弄清楚为什么我的终端会出现此错误 - Can't figure out why I am getting this error in my terminal while creating a webpage with the help of API 我的“t”在控制台中未定义,我不知道我的代码有什么问题。 如何在此代码中定义我的“t” - Am getting my "t" as undefined in the console and i do not know what is wrong with my code. How can I define my 't' in this code 无法弄清楚我的JavaScript代码出了什么问题 - Can't figure out what is wrong with my JavaScript code 无法弄清楚这种行为:while 循环中的 2 个 while 循环 - Can't figure out this behavior: 2 while loops inside a while loop jQuery只能在循环div时在第一个div上工作。 我仍在学习PHP,但我不知道怎么了 - The jQuery works only works on the first div when looping divs. I am still learning PHP and I can't figure out what's wrong 我无法使用JavaScript代码访问CSS样式。 我的代码有什么问题? - I can't access a css style with my javascript code. what is wrong with my code? 我需要使用循环找出数组的每个字符串中有多少个元音字母。 看不出我做错了什么 - I need to find how many vowels are in each string of the array using loops. Can't see what I'm doing wrong 我正在尝试优化第一块代码。 为什么我的方法不起作用? 缩短代码的最佳方法是什么? - I am trying to optimize the first chunk of code. Why doesn't my approach work? What's the best approach to shorten the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM