[英]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: 该功能应:
Take an array as an argument 以数组作为参数
Create and return a new array of all passed array elements as strings 创建并返回所有传递的数组元素的新数组作为字符串
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.