How to Implement Merge Sort in JavaScript

How to Implement Merge Sort in JavaScript the easy way.


// Split the array into halves and merge them recursively 
function mergeSort (arr) {
  if (arr.length === 1) {
    // return once we hit an array with a single item
    return arr
  }

  const middle = Math.floor(arr.length / 2) // get the middle item of the array rounded down
  const left = arr.slice(0, middle) // items on the left side
  const right = arr.slice(middle) // items on the right side

  return merge(
    mergeSort(left),
    mergeSort(right)
  )
}

// compare the arrays item by item and return the concatenated result
function merge (left, right) {
  let result = []
  let indexLeft = 0
  let indexRight = 0

  while (indexLeft < left.length && indexRight < right.length) {
    if (left[indexLeft] < right[indexRight]) {
      result.push(left[indexLeft])
      indexLeft++
    } else {
      result.push(right[indexRight])
      indexRight++
    }
  }

  return result.concat(left.slice(indexLeft)).concat(right.slice(indexRight))
}

const list = [2, 5, 1, 3, 7, 2, 3, 8, 6, 3];
console.log(mergeSort(list)) // [ 1, 2, 2, 3, 3, 3, 5, 6, 7, 8 ];

iOS Deprecated API Usage Warning Ionic using UIWebView

Apple will no longer support web apps that use UIWebView. The apps and libraries need to be migrated to use WkWebView.

The latest Ionic already uses WkWebView, but several Cordova plugins still rely on UIWebView which is a problem.

https://ionicframework.com/docs/v3/wkwebview/

If you’re using Ionic you need to upgrade to iOS Cordova 5

cordova platform remove ios
cordova platform add ios@5.0.0

Also you might need to remove additional plugins that use UIWebView such as inappbrowser.

cordova plugin rm cordova-plugin-inappbrowser

More information about the breaking changes can be found here:

https://cordova.apache.org/news/2018/08/01/future-cordova-ios-webview.html

How to Enable Preflight CORS in PHP for Angular HTTP requests

When testing Ionic or Angular app you might need to set CORS policy to access a service on a different domain. One way to do this in PHP for testing is to send OK responses for all OPTIONS requests. (If you are testing POST and GET requests)

In your php file set:

// change to your app origin
header('Access-Control-Allow-Origin: http://localhost:8100');
header ("Access-Control-Expose-Headers: Content-Length, X-JSON");
header ("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
header ("Access-Control-Allow-Headers: Content-Type, Authorization, Accept, Accept-Language, X-Authorization");
header('Access-Control-Max-Age: 86400');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    // The request is using the POST method
    header("HTTP/1.1 200 OK");
    return;

}

Don’t ship this test code, this is just for your internal testing of http requests.

How to mask and unmask input element password type in Ionic / Angluar

How to mask and unmask input element password type in ionic/angular/js:

  
this.togglePasswordField = function () {
     console.log("toggle password field called");
     if (document.getElementById("passwordElement").type == "password") {
        document.getElementById("passwordElement").type = "text";

         document.getElementById("passwordHideIcon").classList.remove("ion-eye");
         document.getElementById("passwordHideIcon").classList.add("ion-eye-disabled");
     }
     else {
          document.getElementById("passwordElement").type = "password";
          document.getElementById("passwordHideIcon").classList.remove("ion-eye-disabled");
          document.getElementById("passwordHideIcon").classList.add("ion-eye");     
          }
    }

This way only uses basic html element manipulation.

passwordHideIcon element is a button that calls the function and uses the ionic icon for eyes and eyes disabled.

How to Disable Android Back Button in Ionic 1 / Angular

To disable back button in Android put one of the codes below in your app.js in the .run function.

// app.js

// Disable Back in Entire App
$ionicPlatform.registerBackButtonAction(function(){
  event.preventDefault();
}, 100);

Or Conditionally Disable Back:

// app.js

$ionicPlatform.registerBackButtonAction(function(){
  if($ionicHistory.currentStateName === 'someStateName'){
    event.preventDefault();
  }else{
    $ionicHistory.goBack();
  }
}, 100);