Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mohajit2711/JavaScript-Alchemy
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: Week1
Choose a base ref
...
head repository: mohajit2711/JavaScript-Alchemy
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 5 commits
  • 5 files changed
  • 1 contributor

Commits on Dec 15, 2022

  1. Sort Array

    Given an array of numbers, sort the numbers ascending (1,2,3...) and return the sorted array.
    
    In fact, returning any negative value will have the same effect as returning -1 here. Any positive value will do the same as 1. This means we can shorten this function significantly:
    
    [3,2,4,1].sort(function comparison(a,b) {
        return a - b;
    });
    Here, if a is less than b, the result of the subtraction will be negative (placing a first). If b is less than a the result will positive (placing b first).
    mohajit2711 authored Dec 15, 2022
    Configuration menu
    Copy the full SHA
    83adcd2 View commit details
    Browse the repository at this point in the history
  2. Sort Down Array

    Sorting Descending
    In the previous example, we sorted our array ascending:
    
    [3,2,4,1].sort(function comparison(a,b) {
        return a - b;
    });
    A negative result moves a in front of b. A positive result moves b in front of a. Zero keeps the order unchanged.
    
    Sorting descending will be the opposite. We want a negative result to move b in front of a and a positive result to move a in front of b.
    
     Your Goal: Sort Numbers Descending
    Given an array of numbers sort them in descending order and return the array.
    mohajit2711 authored Dec 15, 2022
    Configuration menu
    Copy the full SHA
    bb251a6 View commit details
    Browse the repository at this point in the history
  3. String Compare in Array sort

    Comparing Strings
    Strings have a built-in method for convienent comparison called localeCompare.
    
    'a'.localeCompare('a'); // 0
    'a'.localeCompare('b'); // -1
    "apple".localeCompare("abcd"); // 1
     The localeCompare method also gives options for things like case, accent sensitivity and language. To learn more see the full documentation.
    
    Conveniently, localeCompare returns the numerical values we need to help sort our strings! As shown above, when a string is compared to one that would come after it, the result is -1. When compared to a string that should precede it, the result is 1.
    
     Your Goal: Sort Strings Ascending
    Given an array of strings, sort them in ascending order ('a','b','c'...) and return the sorted array.
    
     For debugging purposes, the tests will log your actual results versus the expected test results.
    mohajit2711 authored Dec 15, 2022
    Configuration menu
    Copy the full SHA
    5f52bdf View commit details
    Browse the repository at this point in the history

Commits on Dec 18, 2022

  1. Strings Ascending

    Comparing Strings
    Strings have a built-in method for convienent comparison called localeCompare.
    
    'a'.localeCompare('a'); // 0
    'a'.localeCompare('b'); // -1
    "apple".localeCompare("abcd"); // 1
     The localeCompare method also gives options for things like case, accent sensitivity and language. To learn more see the full documentation.
    
    Conveniently, localeCompare returns the numerical values we need to help sort our strings! As shown above, when a string is compared to one that would come after it, the result is -1. When compared to a string that should precede it, the result is 1.
    
     Your Goal: Sort Strings Ascending
    Given an array of strings, sort them in ascending order ('a','b','c'...) and return the sorted array.
    
     For debugging purposes, the tests will log your actual results versus the expected test results.
    mohajit2711 authored Dec 18, 2022
    Configuration menu
    Copy the full SHA
    8dc79c7 View commit details
    Browse the repository at this point in the history

Commits on Dec 19, 2022

  1. Sort Students

    Sorting by Multiple Properties
    When it comes to objects, we can sort by their properties.
    
    Let's consider some students:
    
    const students = [
        { id: 1, graduated: true, grade: 86 },
        { id: 2, graduated: false, grade: 96 },
        { id: 3, graduated: false, grade: 78 },
        { id: 4, graduated: true, grade: 96 },
    ];
    Let's sort this list by two rules in the following priority:
    
    Students who Graduated
    Highest Grades
    const students = [
        { id: 4, graduated: true, grade: 96 },
        { id: 1, graduated: true, grade: 86 },
        { id: 2, graduated: false, grade: 96 },
        { id: 3, graduated: false, grade: 78 },
    ];
     Now the list shows the graduated students at the top and then it sorts those groups by the highest grades.
    
     To compare students, this can be broken up into three scenarios.
    
     Your Goal: Sort Students
    Given an array of students, sort first by graduated then by grade like in the example above.
    
    Each object in the students array will have properties id, graduated and grade just like in the example shown above.
    
     For debugging purposes, the tests will log your actual results versus the expected test results.
    mohajit2711 authored Dec 19, 2022
    Configuration menu
    Copy the full SHA
    39db3ce View commit details
    Browse the repository at this point in the history
Loading