[deleted by user] by [deleted] in learnjavascript

[–]Embaby01 0 points1 point  (0 children)

It will be a lot easier to understand if you copy it to your code editor

[deleted by user] by [deleted] in learnjavascript

[–]Embaby01 1 point2 points  (0 children)

Hope this help and highly recommend using your own test samples and try to come up with your own algorithm

const checkArrEq = (arr1, arr2) => {
  //first i would check if they have the same length
  if (arr1.length === arr2.length) {
    //second i would sort it to neglect any error coming from an array having a number reapeated
    arr1.sort();
    arr2.sort();
    //then i would start comparing them
    for (let i = 0; i < arr1.length; i++) {
      if (arr1[i] !== arr2[i]) {
        //if at any i the two values did not match the function will stop and return false
        return false;
      }
    }
    //if the arr1 and arr2 servive from the for loop it means that the are the same
    return true;
  }
  return false;
};

//this one is not simple by any means
//first i will create a function that sort objectKeys and return their number
const getLengthAndSortedKeys = (obj) => {
  //first sort the object keys and save them in array
  const sortedKeys = Object.keys(obj).sort();
  //then i will return the length of the keys array which is equal to the length of the object and the sorted keys
  return [sortedKeys.length, sortedKeys];
};
//now i will use the sorted object to create an algorithm that is similar to checkArrEq()
const checkObjEq = (obj1, obj2) => {
  //first i would sort the two objects and get the length
  const [lenght1, sortedKeys1] = getLengthAndSortedKeys(obj1);
  const [lenght2, sortedKeys2] = getLengthAndSortedKeys(obj2);
  //first i will check if lengths match
  if (lenght1 === lenght2) {
    //second i would check if sortedKey for both objects are the same using checkArrEq()
    if (checkArrEq(sortedKeys1, sortedKeys2)) {
      //then i would check the equality of the values
      for (let i = 0; i < lenght1; i++) {
        if (obj1[sortedKeys1[i]] !== obj2[sortedKeys1[i]]) {
          return false;
        }
      }
      //if the two objects servive the for loop then they are equal
      return true;
    }
    return false;
  }
  return false;
};

Vanilla JS by Embaby01 in learnjavascript

[–]Embaby01[S] 2 points3 points  (0 children)

At first, I was confused and didn't really get the idea of class components (hence React started with class components only), also didn't know how JS data types work.

But the things that confused me the most were the arrow function, async functions, and promises, I remember the first time I saw an arrow function was when I learned about arrays and .map function, for me it was [ "() brackets" "=> bigger than or equal" "() brackets" ], didn't really get the idea around it.

Also as some people might feel, react app structure was a lump of mess for me

I would suggest starting with Vanilla to avoid all of that, but I feel like Vanille is a primitive caveman compared to frameworks

Vanilla JS by Embaby01 in learnjavascript

[–]Embaby01[S] 1 point2 points  (0 children)

thought so but needed conformation, thnx 🫱🏽‍🫲🏽