Learn Array Methods by solving Beecrowd problem

Learn Array Methods by solving Beecrowd problem

We will learn today about four Array methods throughout the article that are used to solve our beecrowd problem. At first, I want to mention brief introduction about these arrays. If you feel bored to read this method, then go to bottom and watch solved code snippet.

  • Push () array method

Appends new elements to the end of an array and returns the new length of the array.

Just like you give an element as argument. Element means it could be string, number, boolean, object, null, undefined and even an array inside an array. The element you have given to him will be placed at the last of the array and returns the new length of the array.

let arr = [1, 2, 3];
arr.push("Torjuman", "Rimon", true);
// To see the output
console.log(arr);
  • Slice() array method

To copy form an array to a new array we could use slice method. We can also define the index number where to start copying, by defualt it starts copying from 0 index.

let arr = [1, 2, "Torjuman", "Rimon" 3, 4];
let newArr = arr.slice(2);
console.log(newArr);
// There are also some other methods to copy an array
arr.concat(something)
arr.push(something)
arr.map(x => +x)
[...arr]
Array.of(...arr)
  • Sort() array method

Sort method will sort the array instantly. This method mutates the array and returns a reference to the same array. Generally, it takes two arguments. See the code you can understand.

let arr = [67, 9, -6];

// To make the array in ascending order
arr.sort((a, b) => a - b);
console.log(arr);

// To make the array in descending order
arr.sort((a, b) => b - a);
console.log(arr);
  • Map() array method

This method Calls a defined call-back function on each element of an array and returns an array that contains the results.

let arr = ["67", "9", "-6"];

arr.map((x) => +x); // (+x) will parse the string to number
console.log(arr);

Beecrowd problem number 1042

Read three integers and sort them in ascending order. After, print these values in ascending order, a blank line and then the values in the sequence as they were read.

Input

  • The input contains three integer numbers.

Output

  • Present the output as requested above.
Input SampleOutput Sample
7 21 -14-14
-7
21
7
21
-14
-14 21 7-14
7
21
-14
21
7

See the Original problem link

Let's take a look at code snippet first

let num = "7 21 -14",
  inputArr = num.split(" ").map((x) => +x),
  ascendingArr = inputArr.slice();
ascendingArr.sort((a, b) => a - b);
let arr = [];

for (let i = 0; i < 3; i++) {
  arr.push(ascendingArr[i]);
}
arr.push("");
for (let i = 0; i < 3; i++) {
  arr.push(inputArr[i]);
}
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

Explanation

I will not explain every single line of code. I will just explain what the array methods are doing there.

  • map()

    • map will traverse every element that are in (inputArr) have and make parse them into number with the help of (+x)
  • slice()

    • at next line the slice makes a copy of the array for (ascendingArr)
  • sort()

    • after slice line, at next line sort will arrange the array (ascendingArr) to ascending order
  • push ()

    • then push all values (ascendingArr and inputArr) to new array so that it can be printed easily

Go to my GitHub link to know more