JavaScript Programming Concepts

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will be exploring the concept of JavaScript programming and its various features and functionalities. Through practical exercises and hands-on programming assignments, we will delve into the core concepts of JavaScript such as functions, arrays, objects, and more. By the end of this lab, you will have a solid understanding of JavaScript and its practical applications in web development.


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/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28493{{"`JavaScript Programming Concepts`"}} javascript/data_types -.-> lab-28493{{"`JavaScript Programming Concepts`"}} javascript/arith_ops -.-> lab-28493{{"`JavaScript Programming Concepts`"}} javascript/comp_ops -.-> lab-28493{{"`JavaScript Programming Concepts`"}} javascript/array_methods -.-> lab-28493{{"`JavaScript Programming Concepts`"}} javascript/spread_rest -.-> lab-28493{{"`JavaScript Programming Concepts`"}} end

How to Calculate the Median of an Array of Numbers

To calculate the median of an array of numbers, follow these steps:

  1. Find the middle of the array.
  2. Use Array.prototype.sort() to sort the values.
  3. If Array.prototype.length is odd, return the number at the midpoint. If it is even, return the average of the two middle numbers.
  4. To start practicing coding and using node, open the Terminal/SSH and type node.

Here's an example code snippet that implements this logic:

const median = (arr) => {
  const mid = Math.floor(arr.length / 2),
    nums = [...arr].sort((a, b) => a - b);
  return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};

You can call this function with an array of numbers as shown below:

median([5, 6, 50, 1, -5]); // 5

Summary

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

Other JavaScript Tutorials you may like