Lkoprivica
Lkoprivica

Reputation: 39

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

Write a function called 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' ]

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

Upvotes: 0

Views: 80

Answers (4)

Canilho
Canilho

Reputation: 1209

When you enter the last loop, the i becomes equal to array.length, so array[i] is undefined after this. 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]));

Upvotes: 0

李德康
李德康

Reputation: 15

If the arraylenth is 10, the function will return array[10].This is an array-bound.

Upvotes: 0

Louis Durand
Louis Durand

Reputation: 335

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. 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.

  • 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. (Tip: var new_array = []; to create, and use new_array.push(elem); to append an element)

  • 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. You loop while(index < array.length).

At the end, you can return the new array !

Good luck with learning programming!

Upvotes: 0

Amadan
Amadan

Reputation: 198334

Two Three errors.

Firstly, the while loop will exit when i is no longer less than array.length; the first such number is array.length. This means, at the end of the loop, array[i] is array[array.length], which is just outside the array, thus 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.

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:

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

Upvotes: 2

Related Questions