1

I'm trying to obtain // [2,6,0,8,4] from the function:

let getValidPassword = arr => {
  let x = [];
  for (let i in arr) {
    for (let j in arr[i]) {
      if (arr[i][j] % 2 !== 0) {
        break;
      } else {
        x += arr[i][j];
      }
    }
  }
  return x
};

var loggedPasscodes = [
  [1, 4, 4, 1],
  [1, 2, 3, 1],
  [2, 6, 0, 8],
  [5, 5, 5, 5],
  [4, 3, 4, 3]
];
console.log(getValidPassword(loggedPasscodes)); 

However when I run the typeof x, I'm getting a string(I though it was a number) and when I print x I get 26084 instead of [26084] what witchcraft is this? I though setting x to [ ] would make the trick... thank you.

Tiago Ruivo
  • 183
  • 1
  • 9
  • 1
    Possible duplicate of [How to append something to an array?](https://stackoverflow.com/questions/351409/how-to-append-something-to-an-array) – Ivar Oct 25 '19 at 16:48

3 Answers3

0

The problem is that you are incrementing your variable, to add an element to an array you need to use the push() method.

Correct code:

let getValidPassword = arr => {
  let x = [];
  for (let i in arr) {
    for (let j in arr[i]) {
      if (arr[i][j] % 2 !== 0) {
        break;
      } else {
        x.push(arr[i][j]);
      }
    }
  }
  return x
};

var loggedPasscodes = [
  [1, 4, 4, 1],
  [1, 2, 3, 1],
  [2, 6, 0, 8],
  [5, 5, 5, 5],
  [4, 3, 4, 3]
];
console.log(getValidPassword(loggedPasscodes)); 
Guilherme Martin
  • 837
  • 1
  • 11
  • 22
  • So adding transforms the array into a string even if I have [ ]? – Tiago Ruivo Oct 25 '19 at 16:54
  • It will print as an array. Test this code I sent you, it is working correctly. – Guilherme Martin Oct 25 '19 at 16:55
  • 1
    @TiagoRuivo Using `+` to an array turns that array into a string. (And every iteration after that `x` will be a string and adding a number to a string also results in a string.) See https://stackoverflow.com/questions/9300941/why-is-the-result-of-adding-two-empty-arrays-in-javascript-a-string – Ivar Oct 25 '19 at 16:56
  • I still have a problem. I just realize that I need [2, 6, 0, 8] , and I'm getting [2, 6, 0, 8, 4]. I don't know where this 4 is coming from... – Tiago Ruivo Oct 25 '19 at 16:58
  • @TiagoRuivo Because you have it in your array, and it counts as an even number. Thus it falls into the else block of your code and is added to the array. – Guilherme Martin Oct 25 '19 at 17:21
  • yes I know that. I just don't know how to change it. tried a few times but so far nothing... – Tiago Ruivo Oct 25 '19 at 17:22
  • In this case it would only resolve to remove the element from array. – Guilherme Martin Oct 25 '19 at 17:24
0

When you concatenate an array (which is what += is doing) it first converts the array and the value being appended to strings.

In order to add elements to the x array use x.push(arr[i][j]), this will insert them without type conversion.

Ivar
  • 6,138
  • 12
  • 49
  • 61
JDunken
  • 465
  • 2
  • 7
  • in javascript, a string is an array of of individual characters. This is why `'some string'.length`, returns a value. – JDunken Oct 25 '19 at 17:08
  • So, a string is not strictly an array, but for the most part, it behaves as one. – JDunken Oct 25 '19 at 17:23
0

The problem here is that you have declared x=[] but you are modifying it as x += arr[i][j]; as soon as javascript gets to this line. It treats the array as string calling x.toString() internally and appending to that string. For example if you declare an array as a=[] and call a+=1 then a will become "1". In javascript everything is value typed, it doesn't matter what you declare when you assign some value to it or do some operation on the variable, it gets converted to the type of value.

I would recommend you to go through this

let getValidPassword = arr => {
 let x = [];
 let temp = [];
 for (let i in arr) {
  for (let j in arr[i]) {
   if (arr[i][j] % 2 !== 0) {
    break;
   } else {    
    temp.push(arr[i][j]);    
   }
   if(temp.length == arr[i].length)
    x = temp.slice(); 
  }
 }
 return x
};

var loggedPasscodes = [
[1, 4, 4, 1],
[1, 2, 3, 1],
[2, 6, 0, 8],
[5, 5, 5, 5],
[4, 3, 4, 3]
];
console.log(getValidPassword(loggedPasscodes)); 
Akshay Bande
  • 2,491
  • 2
  • 12
  • 29
  • thanks I did just that. And it works. The problem is that I need to return [2,6,0,8] and I'm getting [2,6,0,8,4]. 4 is the next iteration that passes the test so it's added to the array. I tried to just x.push(arr[i]) but doesn't really work... Any ideas? I should return the 3rd array... – Tiago Ruivo Oct 25 '19 at 17:11
  • You can do it by using a temporary array and checking its length after every iteration of the first loop is completed. I have updated my answer according to it. – Akshay Bande Oct 25 '19 at 17:18
  • I did something similar but instead of creating new var I just put a condition on the else statement : else { if (x.length < 4) { x.push(arr[i][j]); – Tiago Ruivo Oct 25 '19 at 17:26
  • It worked, but I'm still not passing the test... This is an exercise for an exam... – Tiago Ruivo Oct 25 '19 at 17:28
  • haha... what is required to pass the test? you got the answer, what else is required? – Akshay Bande Oct 25 '19 at 17:30
  • ok. I got it. I can't have it hard coded like that. your solution passed. turns out I was wrong... Printing the same thing doesn't always mean that is the same thing... Still a lot to learn... – Tiago Ruivo Oct 25 '19 at 17:30