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…!

1 Like

@Leanne_Smith
you very welcome
could you till me where you stuck
and no matter how hard things is it will get easier the more you practice and the more you learn
and have a nice day :slight_smile:

1 Like

Hi @justsomeone

Glad you are part of the MDN community :star:

Ran

1 Like

hi @Ran

the honor is mine to be part of the MDN and to meet all of you

trust me reading others code help a lot it help to see different way to solve same issue and also it help to make me review the topics i learn so sometime you did not use certain topic for while which will make you forget it

also some people share other courses task or maybe their school task which make you see difference challenge and even search and learn more topics that maybe i did not think of before

and i really appreciate your nice words which i do not think i deserve it and i see you also help many people and give them very detailed comment to help
them
so thanks for you for being one of the MDN family

thanks a lot and have a nice day :slight_smile:

1 Like

I coundnt agree more @justsomeone. I do this because I enjoy helping others as well as learning from others. This is a nice community so I hope that it will grow and we can become more proficient in our own skills.

I am taking a bit of a break from the MDN side will I look into Scrimba to get some further clarity and challenges but I think MDN is the best resource for my future plans to become a front end web dev… well sort of. I m just learning this so I can build my own tools for work.

In the mean time I plan to learn and work with everyone on MDN.

1 Like

thanks for sharing this course and good luck :slight_smile:
and i agree that the community here is awesome

1 Like

Hi, this will work on JsHero.

function flat(array) {

return array.flat();

}
console.log(flat([[4,9,2],[3,5,7],[8,1,6]]));

1 Like