JS/TS
  • JavaScript Development
  • JS Principles
    • JS Principles
      • Primitive data types
      • typeof operator
      • Scope
      • Hoisting
      • IIFE
      • Closure
      • Anonymous functions in JS
      • Conditional (ternary) operator
        • Coercion vs Conversion
      • Event-driven programming
      • Factory Function
      • JSON.stringify()
      • Strict mode
      • super() keyword
      • What are memory leaks?
      • Micro-tasks within an event loop (Summary)
      • Macro-tasks within an event loop (Summary)
      • null vs undefined
    • Memory Management
    • Advanced function concepts
      • Impure vs Pure Functions
      • Factory functions
  • JavaScript Objects & Arrays
    • Introducing JavaScript objects
      • Build-in objects
        • isNaN()
      • RegExp
        • RegExp.prototype.test()
      • String
        • String.prototype.split()
        • String.prototype.slice()
      • Objects
        • Object.assign()
        • Object.create()
        • Object.defineProperties()
        • Object.defineProperty()
        • Object.entries()
        • Object.freeze()
        • Object.getOwnPropertyNames()
        • Object.getPrototypeOf()
        • Object.isFrozen()
        • Object.isSealed()
        • Map
      • Standard built-in methods to work with Arrays
        • Array.of()
        • Array.prototype.concat()
        • Array.prototype.every()
        • Array.prototype.filter()
        • Array.prototype.find()
        • Array.prototype.findIndex()
        • Array.prototype.forEach()
        • Array.prototype.join()
        • Array.prototype.map()
        • Array.prototype.pop()
        • Array.prototype.shift()
        • Array.prototype.reverse()
        • Array.prototype.some()
        • Array.prototype.sort()
        • Array.prototype.splice()
        • Array.prototype.unshift()
        • Array.prototype.includes()
        • Array.prototype.flatMap()
      • Prototypal inheritance
        • Inheritance with the prototype chain
        • Inheriting "methods"
  • JavaScript Mid
    • JavaScript & ES
      • Arrow Function
      • Anonymous Function
      • Callbacks
      • Promises
      • var, let, and const
      • Fetch API (function)
      • Fetch API
      • Synchronous vs Asynchronous
      • Encapsulation
      • Destructuring assignment
      • call() - apply() - bind()
      • 'This' keyword
      • Functional Programming
  • Browser
    • Event-driven programming
  • TypeScript
    • The TypeScript Handbook
      • Basic Types
      • Interfaces
      • Functions
      • Literal Types
      • Unions and Intersection Types
      • Classes
      • Enums
      • Generics
      • Implements vs extends
  • Hackerrank Practices
    • Practices and examples
  • JS Math
    • Mathematical
      • JavaScript | Math.E() function
      • Math.abs( ) Method
      • Math.ceil( ) function
      • Math floor()
      • Math.imul( ) Function
      • Math log( ) Method
      • Math max()/min() Method
      • Math pow( ) Method
      • Math.sign( ) Function
      • Math sqrt( ) Method
Powered by GitBook
On this page
  • Reference link click here
  • Practice 1
  • Practice 2
  • Practice 3
  • Practice 4
  • Practice 5
  • Practice 6
  • Practice 7
  • Practice 8
  • Practice 9

Was this helpful?

  1. Hackerrank Practices

Practices and examples

PreviousImplements vs extendsNextMathematical

Last updated 4 years ago

Was this helpful?

Reference link click

Practice 1

"Fibonacci Sequence" redirects here. For the chamber ensemble, see A tiling with squares whose side lengths are successive Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, and 21.

In mathematics, the Fibonacci numbers commonly denoted Fn, form a , called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,{\displaystyle F_{0}=0,\quad F_{1}=1,}

and{\displaystyle F_{n}=F_{n-1}+F_{n-2}}

for n > 1.

The beginning of the sequence is thus

{\displaystyle 0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\;\ldots }
function fibonacci(maxNumber) {
    const fib = [];
    let fibRes = [];

    fib[0] = 0;
    fib[1] = 1; 
    for (let i = 2; i <= maxNumber; i++) {
        fib[i] = fib[i - 2] + fib[i - 1];
        fibRes.push(fib[i]);
    }
    console.log(fibRes);
}

Practice 2

Create a function that will receive two arrays and will return an array with elements that are in the first array but not in the second.

function elementSelector(arr1, arr2) {
    let myArray = [];
    for (let index = 0; index < arr2.length; index++) {
        if (!arr1.includes(arr2[index])) {
            myArray.push(arr1[index]);
        }
    }
    console.log(myArray);
}

Practice 3

Create a function that will receive two arrays of numbers as arguments and return an array composed of all the numbers that are either in the first array or second array but not in both.

function mergeArrays(arr1, arr2) {
    let myArray = [];
    for (let index = 0; index < arr2.length; index++) {
        if (!arr1.includes(arr2[index])) {
            myArray.push(arr2[index]);
        }
    }

    for (let index = 0; index < arr1.length; index++) {
        if (!arr2.includes(arr1[index])) {
            myArray.push(arr1[index]);
        }
    }
    console.log(myArray);
}

Practice 4

Imagine you have an array of objects:

const myArray = [
{
    'id': 1,
    'name': 'John'
},
{
    'id': 2,
    'name': 'Bob'
}];

How to turn this array into an object where ids are keys? E.g.

const myObject =
{
    '1':
    {
        'id': 1,
        'name': 'John'
    },
    '2':
    {
        'id': 2,
        'name': 'Bob'
    }
}

Result

function fromArrayToObject(arr) {
    let myObject = {};
    myObject = Object.assign({}, arr);
    console.log(myObject);
}

and backwards:

function fromObjectToArray(obj) {
    let myArray = Object.values(obj);
    console.log(myArray);
}

Practice 5

Calculate the average of the numbers in an array of numbers

function calculateAverage(arr) {
    let total = 0;
    let n = arr.length;
    for (let i = 0; i < n; i++) {
        total += arr[i];
    }
    average = (total / n);
    return (Math.round(average));
}

Practice 6

Create a function that will receive n as argument and return an array of n random numbers from 1 to n.

// Create a function that will receive n as argument and return an array of n
// random numbers from 1 to n. The numbers should be unique inside the array.
function randomize(num) {

    let myArray = [];

    for (let i = 1; i <= num; i++) {
        if (!myArray.includes(i)) {
            myArray.push(i);
        }
    }
    return myArray;
}

Practice 7

Reverse a string

// Reverse a string
function reverseString(word) {
    let myWord = "";
    for(let i = word.length -1; i >= 0; i--) {
        myWord += word[i];
    }
    return myWord;
}

Practice 8

Reverse an array using build-in method and non-build in.

// Reverse an array
function reverseArray(arr) {
    // return arr.reverse(); // Build-in method
    let myArray = []; 
    for (let i = arr.length - 1; i >= 0; i--) {
        myArray.push(arr[i]);
    }
    return myArray;
}

Practice 9

Create a function that receives an array of numbers as argument and returns an array containing only the positive numbers

// Create a function that receives an array of numbers as argument and returns an
// array containing only the positive numbers
function getPositiveNumbers(numArr) {
    let myArray = [];
    // First way
    // for (let i = 0; i < numArr.length; i++) {
    //     if (numArr[i] > 0) {
    //         myArray.push(numArr[i]);
    //     }
    // }

    // Second way
    myArray = numArr.filter(x => x > 0);
    return myArray;
}
here
Fibonacci Sequence (ensemble)
sequence
[1]
{\displaystyle F_{0}=0,\quad F_{1}=1,}
{\displaystyle F_{n}=F_{n-1}+F_{n-2}}