Exploring JavaScript Fundamentals Through Exercises

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the fundamentals of JavaScript programming. Through a series of exercises and challenges, we will cover topics such as variables, data types, functions, control flow, and more. By the end of this lab, you will have a solid understanding of JavaScript syntax and be able to write basic programs in this versatile programming language.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28566{{"`Exploring JavaScript Fundamentals Through Exercises`"}} javascript/data_types -.-> lab-28566{{"`Exploring JavaScript Fundamentals Through Exercises`"}} javascript/arith_ops -.-> lab-28566{{"`Exploring JavaScript Fundamentals Through Exercises`"}} javascript/comp_ops -.-> lab-28566{{"`Exploring JavaScript Fundamentals Through Exercises`"}} javascript/cond_stmts -.-> lab-28566{{"`Exploring JavaScript Fundamentals Through Exercises`"}} javascript/array_methods -.-> lab-28566{{"`Exploring JavaScript Fundamentals Through Exercises`"}} javascript/higher_funcs -.-> lab-28566{{"`Exploring JavaScript Fundamentals Through Exercises`"}} javascript/spread_rest -.-> lab-28566{{"`Exploring JavaScript Fundamentals Through Exercises`"}} end

Quick Sort Algorithm

To practice coding, open the Terminal/SSH and type node. This algorithm sorts an array of numbers using the quicksort algorithm. Here are the steps to follow:

  • Use recursion.
  • Use the spread operator (...) to clone the original array, arr.
  • If the length of the array is less than 2, return the cloned array.
  • Use Math.floor() to calculate the index of the pivot element.
  • Use Array.prototype.reduce() and Array.prototype.push() to split the array into two subarrays. The first one contains elements smaller than or equal to pivot, and the second one contains elements greater than it. Destructure the result into two arrays.
  • Recursively call quickSort() on the created subarrays.

Here is an example of how to implement this algorithm:

const quickSort = (arr) => {
  const a = [...arr];
  if (a.length < 2) return a;
  const pivotIndex = Math.floor(arr.length / 2);
  const pivot = a[pivotIndex];
  const [lo, hi] = a.reduce(
    (acc, val, i) => {
      if (val < pivot || (val === pivot && i != pivotIndex)) {
        acc[0].push(val);
      } else if (val > pivot) {
        acc[1].push(val);
      }
      return acc;
    },
    [[], []]
  );
  return [...quickSort(lo), pivot, ...quickSort(hi)];
};

To test it out, run the following command:

quickSort([1, 6, 1, 5, 3, 2, 1, 4]); // [1, 1, 1, 2, 3, 4, 5, 6]

Summary

Congratulations! You have completed the Quick Sort lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like