Help for Array within Array

Hi,

I’ve come across two challenges so far on JS Hero that i cant complete.
Some of them dont have a solution so Im looking for help.

the first one is:

Exercise

Write a function flat that flattens a two-dimensional array with 3 entries.

Example: flat(loshu) should return [4, 9, 2, 3, 5, 7, 8, 1, 6] . Thereby loshu is the magic square from the example above.

My Code:
I’ve put is JS console, and it works, so i cant see why it is wrong…

function flat (array){

let array1 = array[0];
let array2 = array[1];
let array3 = array[3];
let array4 = array1.concat(array2);
let array5 = array4.concat(array3);
return array5;

}

ANS:

Reading your code successfully.

flat is a function.

flat has 1 parameter(s).

flat([[], [], []]) does not return [] , but [ undefined ] .

Test-Error! Correct the error and re-run the tests!

The second one is

Exercise

Write a function xor that takes two Boolean values. If both values are different, the result should be true . If both values are the same, the result should be false .

I.e.: The calls xor(true, false) and xor(false, true) should return true . The calls xor(true, true) and xor(false, false) should return false .

function xor ( a,b ) {

let or = a && !b
return or;

}

ANS:

Reading your code successfully.

xor is a function.

xor has 2 parameter(s).

xor(true, true) returns false .

xor(false, false) returns false .

xor(true, false) returns true .

xor(false, true) does not return true , but false .

Test-Error! Correct the error and re-run the tests!

Thanks !!

Hi @Leanne_Smith
This looks very interesting. I will have a look at it this weekend, sadly a bit busy.

For anyong looking at this the jshero sections are

Array of arrays
XOR

I have never heard of jshero. Thanks for sharing this in the MDN community :pray:t4::sunglasses:

Hello @Leanne_Smith
2 issue on first one

i tested it with the following code

function flat(array) {

  let array1 = array[0];
  let array2 = array[1];
  let array3 = array[2];
  let array4 = array1.concat(array2);
  let array5 = array4.concat(array3);
  return array5;

}
console.log(flat([['Apple', 'Banana'],['jhle', 'jhgj'],['76b87', 'trtyrty']]))

your code

 let array3 = array[3];

which should be

 let array3 = array[2];

the second issue is this way you only concat 3 array so if the paramter include array of 4 or moore arrays it will concat only the first 3 and ignore the rest
so try to loop with the array length you get from the function paramter then do the concat
also this will solve the issue if there was only 2 or 1 array
for example

function flat(array) {

 let resultArray = array[0];
for (let i = 1; i < array.length; i++) {
resultArray = resultArray.concat(array[i]);
}
  return resultArray;

}

hope this help

now for second one

function xor ( a,b ) {

return !(a===b);

}

hope that help and have a nice day :slight_smile:

1 Like

Thank you so much for taking the time to get back to me. I really appreciate it!
I was working through the JSHero which all seemed pretty straight forward until the later stages, now I’m getting stuck more and more and there are no solutions for a work around :frowning:

It very tricky this learning Javascript malarky… any tips on how i can improve and get my head around things…!