Freecodecamp JavaScript elementary algorithm question-3

13. Filter false values in the array
Remove all false values from the array
False values in JavaScript include false, null, 0, "", undefined and NaN.
Tip: consider converting each value to a boolean.
Input: bouncer([7, "ate", "", false, 9])
Output: [7, "ate", 9]
Solution 1:

function bouncer(arr) {
  let temp = [];
  for(let i =0;i<arr.length;i++) {
    if(Boolean(arr[i])=== true) {
      temp.push(arr[i]);
    }
  }
  return temp;
}

Idea:
Very simple, create a new temporary array, traverse each item in the array, judge whether its Boolean value is true, and then put the true item into the temporary array.
Knowledge point: judge whether the Boolean value of a variable is true: Boolean (variable); Add elements to the array, push()

Solution 2:

function bouncer(arr) {
  let newArray = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i]) newArray.push(arr[i]);
  }
  return newArray;
}

Idea: the reconciliation method is the same, but it doesn't use the Boolean method to judge

Solution 3:

function bouncer(arr) {
  return arr.filter(Boolean);
}

Idea: using js filtering method: failure

14. Find the index of the element in the sorted array
Array (first parameter) after sorting, insert a value (second parameter) into the array and keep the array in order. Returns the minimum index value of the newly inserted element. The return value should be a number.

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because 1.5 is greater than 1 (index 0) and less than 2 (index 1).

Similarly, getIndexToIns([20,3,5], 19) should return 2. Because the array will become [3,5,20] after sorting, while 19 is less than 20 (index 2) and greater than 5 (index 1).
Solution:

function getIndexToIns(arr, num) {
  arr.sort((a, b) => a - b);
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] >= num)
      return i;
  }
  return arr.length;
}

Idea:
Use the sort method of js to sort the array, and then traverse the array. If the element is greater than num, return its subscript.

15. Compare strings
Returns true if the first string in the array contains all the letters in the second string.
For example, ["hello", "Hello"] should return true. Because in the case of ignoring case, the first string contains all the letters in the second string.
["hello", "hey"] should return false. Because Hello does not contain the character y.
Finally, ["Alien", "line"] should return true. Because all the letters in aline appear.
Solution:

function mutation(arr) {
  let test = arr[1].toLowerCase();
  let target = arr[0].toLowerCase();
  for (let i = 0; i < test.length; i++) {
    if (target.indexOf(test[i]) < 0) return false;
  }
  return true;
}

Idea:
Because the title requires to ignore case, you should first turn it to lowercase and put it in the new array to traverse the test array. If the target array does not contain letters, it will return false, otherwise it will return true if it contains all. indeOf here can also be written as
target.indexOf(test[i]) == -1

Knowledge points: ① to lowercase: toLowerCase()
② Whether the array contains a value, indexOf. If it contains, its subscript will be returned. If it does not contain, it will return - 1

16. Split array
Please write a function that splits an array (the first parameter) into several sub arrays with length of size (the second parameter) and returns them as two-dimensional arrays.
Input: chunkArrayInGroups(["a", "b", "c", "d"], 2)
Output: ["a", "b"], ["c", "d"]]
Solution:

function chunkArrayInGroups(arr, size) {
  let newArr = [];
  for (let i = 0; i < arr.length; i += size) {
    newArr.push(arr.slice(i, i + size));
  }
  return newArr;
}

Idea:
Create a new array to store the segmented array elements, traverse the original array, divide it with slice method, and put these values into the new array every time.

Keywords: Javascript Algorithm

Added by kc11 on Wed, 02 Feb 2022 23:01:54 +0200