@thandomncube3480

I'm starting to reconsider this career. My goodness Recursion ! LOLOL!

@Abulkhair1995

love you nader you deserve the best

@thandomncube3480

I was able to smash to warm-up actually and flatten the array. Still need more practice for myself I must say.

@mdd972

Thank you Nader for the amazing content! 
I've been following the entire javascript series and this lesson is the hardest so far,  I feel like I understand "recursion' but when it comes to the exercices I really struggled

@tomboolery

I'm not even gonna lie these exercises hurt. I thought I had a good enough grasp on the topic just off of the lecture but it's clear this is just going to be something I need to practice more.

I was only able to solve two of these exercises, with one being the warmup and the other being exercise 2. The thing is I solved exercise 2 in an entirely differently way and kind of feel like I cheated (?) after seeing your solution:

// GOAL: Create a function that takes an array as its argument. The function must return a flattened version of the array no matter how NESTED it is.

const flatten = (arr) => {
  // Array.isArray() evaluates whatever value is passed into it and determines whether or not it is an array!

  if (Array.isArray(arr) === true) {
    return console.log(`${arr}`);
  }

  if (Array.isArray(arr) === false) {
    return console.log(`${arr}, is not an array`);
  }

  // .flat() is an ARRAY METHOD that flattens all nested arrays. It creates a new, flattened array =)

  return flatten(arr.flat(arr));
};

console.log(flatten([1, 2, 3]));
console.log(flatten([1, 2, 3, [1, 2, 3]]));
console.log(flatten([1, [4, 5, 6, [7, 8, 9]], 2, 3]));
console.log(flatten("ayo"));


I'm hoping to move onto react and the DOM series soon but I really want to ensure my JS is as sharp as it can be. Feeling humbled. Tough stuff. Thanks as always Nader.

@frq9293

Took me ages but this was my solution for flatten... I was trying to not use loops at all for some reason

    function flatten(arr, i=0){
        if(i===arr.length-1) return arr
        if(!Array.isArray(arr[i])) return flatten(arr, i+1)
        const sub = arr[i];
        arr.splice(i, 1)
        arr.splice(i, 0, ...sub)
        return flatten(arr, i)
    }

@erahia8702

just wanted to say your videos are really helpful, thanks!

@Gerrrawwrr

Hi Nader! I used a ready function for exercise #2. Well i used the "Array.flat()".  And here it is my code :

const flatten = (arr) => {
  array_flatened = arr.flat();
  for (element of array_flatened) {
    if (typeof element === "object") {
      flatten(array_flatened);
    }
  }

  return array_flatened;
};

@timothyrees6318

Great video as always! But very difficult topic. Took me many hours. This is my solution to exercise 2:
let flat = [];
const flatten = (arr)=>{
    for(let i = 0; i<arr.length;i++){
        if(!Array.isArray(arr[i])){
            flat.push(arr[i])
        }
        else flatten(arr[i])
    }
    return flat;
    }

@Kerwell

i think the hardest thing about recursion is the mentality you have to adopt. Took me 2 days and fully gave up on bonus exercise. It makes sense in retrospect, but it takes me a really long time to think in recursion. 

ex1.
const palindrome = (string) => {
  if (string.length === 0) return false;
  if (string.length === 1) return true;
  if (string[0] === string[1]) return true;
  if (string[0] !== string[string.length - 1]) return false;
  if (string[0] === string[string.length - 1]) {
    let arr = string.split(""); // convert string to array
    arr.pop();
    arr.shift();
    string = arr.join("");
    return palindrome(string);
  }
};

ex2. this one was no joke, i just assume we couldn't use any loops until I saw how you solved it lol...
const flatten = (array) => {
  if (array.length === 0) {
    return array;
  }
  if (Array.isArray(array[0])) {
    array.splice(0, 1, ...array[0]);
    return flatten(array);
  }
  return [array[0], ...flatten(array.slice(1))];
};

ex3. 

const flatten = (object) => {
  let flattened = {};
  for (const [key, value] of Object.entries(object)) {
    if (typeof value === "object") {
      flattened = { ...object[key] };
      delete object[key];
      return flatten((flattened = { ...object, ...flattened }));
    }
  }
  return object;
};

I fully gave up on bonus exercise. It was easy in retrospect, but it's really hard for me to adopt the thought process.

@kamilzmuda694

Great channel and videos! πŸ˜„

It's a real pleasure to learn following your tutorials.

I would like to get some feedback about my solution for exercise 2 - flatten the array. Let me know what you guys think! πŸ˜… 

CODE:

const flatten = (arr) => {
  if (!arr.some(element => Array.isArray(element))) {      // base case - check if at least 1 element of passed array is an array, if not return we arr
    return arr;
  } else {
    arr = arr.flat()               // if any of the elements is an array we assign arr.flat( ) to arr and make a recursive call by passing arr flattened by 1 level
    return flatten(arr)    
  } 
}

@aissanadjemeddine1330

Thanks for this amazing video , very helpful

@jjjj5452

this one kicked my butt

@644sixfortyfour4

With these advanced topics, I'm starting to really appreciate your long videos Nader, especially the exercises, so thank you for putting so much into these videos. πŸ˜‡ 
I did take my time to solve the exercises myself first, and went through your solutions after, but I will need more time to master it though.

@ShivamSharma-dq4pu

function flatten(array) {
  let result = [];
  for (const item of array) {
    if (Array.isArray(item)) {
      result = [...result, ...flatten(item)];
    } else {
      result.push(item);
    }
  }

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

@dimadeloseros1

Dude keep it up. Your videos are extremely helpful to me. I love your way of teaching / followed by a video of exercises. 
I'm sure that you will get lots of subs in no time.

@kodyn384

Here's my solution to Exercise 2:

function flatten(arr) {
  const nextNestedArray = arr.find(el => Array.isArray(el));
  const indexOfNextNestedArray = arr.indexOf(nextNestedArray);

// Base cases:
  if(arr.length === 0 || !nextNestedArray) return arr;

// Recursive case:
  let newArr = arr.toSpliced(indexOfNextNestedArray, 1, ...nextNestedArray);
  return flatten(newArr);
}

@ThanhTran-vn8tp

Here is my version of the bonus exercise:
const constructDOM = (object) => {
    let result = "";

    for (const [key, value] of Object.entries(object)) {
        if (key === "nodeName") {
            result += `<${value}>`;
        }
        if (key === "childNodes") {
            for (const obj of value) {
                result += constructDOM(obj);
            }
            if (Object.hasOwn(object, 'nodeName')) {
                result += `</${object.nodeName}>`;
            }
        }
        if (key === "innerText") {
            result += `${value}</${object.nodeName}>`;
        }
    }
    return result;
}

@EliHacΔ±yev-p7v

Thanks 😊

@akashrattan2975

Love the video, in-depth exercises, and everything on the video πŸ’― overall!!!!! Thanks for the constant uploads 😊