2023-05-28

Javascript :: comparison operators, using == vs === and != vs !==

Comparison operators are distinguished following simple logic, whether data type is being included or not.

Essentially if it is === or !==, then the comparison includes a type verification.

Example:

var numberVar = 1;

var stringVar = "1";

//compare if equal

var equalsnumberVar == stringVar; //equals will be set to true

var stricterEqualsnumberVar === stringVar; //stricterEquals will be set to false

//compare if different

var diffnumberVar != stringVar; //diff will be set to false

var stricterDiff = numberVar !== stringVar; //stricterDiff will be set to true


2023-05-10

SQL :: Using Joins (INNER JOIN, LEFT JOIN, RIGHT JOIN)

Understanding joins in SQL can save a lot of pain, the basics of any query is:

SELECT stuff
FROM table

When the table isn't enough, joins start showing up:

SELECT stuff, moreStuff
FROM table
INNER JOIN anotherTable ON table.key = anotherTable.foreignKey


The basics on how joins work:

  • INNER JOIN
    selects all records where table.key = anotherTable.foreignKey

SELECT stuff, moreStuff
FROM table
INNER JOIN anotherTable ON table.key = anotherTable.foreignKey


  • LEFT (OUTER) JOIN
    selects all records from table and any matches from anotherTable where anotherTable.foreignKey = table.key

SELECT stuff, moreStuff
FROM table
LEFT JOIN anotherTable ON table.key = anotherTable.foreignKey


  • RIGHT JOIN
    selects all records from anotherTable and any matches from table where table.key = anotherTable.foreignKey

SELECT stuff, moreStuff
FROM table
LEFT JOIN anotherTable ON table.key = anotherTable.foreignKey





Javascript :: Math round, ceiling (ceil), floor, max, min, random, etc...

It's common to try to remember what's the right call for Math functions, hence adding this to my local KB here.

Always the same principle, Math.function(<params>).

Round:
var a = 123.45;
var b = 345.67;
console.log(Math.round(a));
console.log(Math.round(b));
//outputs: 123
//outputs: 346


Ceiling (ceil):
var a = 123.45;
var b = 345.67;
console.
log(Math.ceil(a));
console.log(Math.ceil(b));
//outputs: 124
//outputs: 346

Floor:
var a = 123.45;
var b = 345.67;
console.
log(Math.floor(a));
console.log(Math.floor(b));
//outputs: 123
//outputs: 345

Max:
var a = 123.45;
var b = 345.67;
console.
log(Math.max(a, b));
//outputs: 345.67

Min:
var a = 123.45;
var b = 345.67;
console.
log(Math.min(a, b));
//outputs: 123.45

Random:
function getRandomInteger(limitIntValue) {
  return Math.round(Math.random() * limitIntValue);
}
function getRandomNumber(limitNumberValue) {
  return (Math.random() * limitNumberValue);
}
console.log(getRandomInteger(10));
console.log(getRandomInteger(-10));
console.log(getRandomNumber(123.45));
//outputs: <random integer value between 0 and 10>
//outputs: <random integer value between -10 and 0>
//outputs: <random number value between 0 and 123.45>


Javascript :: Sort array of objects / Custom sorting

A common issue is handling array sorting when unable to test/validate the code as is common with low-code tooling.

To that end, sorting can usually be done by setting a comparison function.

The comparison function takes two values as input and must return a value with the following rules:

input: two values, for the examples below these will be named itemA and itemB

output: a numeric value that when it's 0 no changes for the positions of itemA and itemB in relation to each other, if positive (>0) then itemA will be placed before itemB and if negative, itemB will be placed before itemA.


The items in the parameters can be objects or values, it's a matter of using the relevant fields/members for the object to deliver a return value.

Comparer function examples:

  • for values:

function compareItems(itemAitemB) {
  if (
itemA itemBreturn 1;
  if (itemA itemBreturn -1;
  return 0; // if it's not one of the above, they're the same
}

  • for objects:

function compareItems(itemAitemB) {
  if (
itemA.numberVal itemB
.numberValreturn 1;
  if (itemA.numberVal itemB.numberValreturn -1;
  return 0; // if it's not one of the above, they're the same
}

Fully working examples below:

  • Using a compare function:

function compareItems(itemAitemB) {
  if (
itemA itemB) return 1;
  if (itemA itemBreturn -1;
  return 0; // if it's not one of the above, they're the same
}

var myArray = [3, 2, 4, 1];
myArray.sort(compareItems);
console.
log(JSON.stringify(myArray));
//outputs: [1,2,3,4]


  • Using an inline comparison function:

var myArray = [3241];
myArray.sort(function compareItems(itemAitemB) {
  if (itemA itemBreturn 1;
  if (itemA itemBreturn -1;
  return 0// if it's not one of the above, they're the same
}
);
console.
log(JSON.stringify(myArray));
//outputs: [1,2,3,4]


  • Using an inline comparison arrow function:

var myArray = [3241];
myArray.sort((itemAitemB) => {
  if (itemA itemBreturn 1;
  if (itemA itemBreturn -1;
  return 0// if it's not one of the above, they're the same
}
);
console.log(JSON.stringify(
myArray));
//outputs: [1,2,3,4]

2023-05-08

Javascript :: Sort array of strings (or numbers) ascending and descending

Sorting an array of strings is a common scenario and it's almost mostly enough to just use sort():

var myArray = ["3", "2", "4", "1"];
myArray.sort();
console.log(JSON.stringify(myArray));
//outputs: ["1","2","3","4"]

However if doing descending order, need to use a comparison function:

// descending order
var myArray = ["3", "2", "4", "1"];
myArray.sort(function (a, b) {
    if (a > b) {
        return -1;
    }
    if (b > a) {
        return 1;
    }
    return 0;
});
console.log(JSON.stringify(myArray));
//outputs: ["4","3","2","1"]

Look into the Sort keyword for other variants.

Javascript :: Add an item at any position in an array

It's easy, just use splice():
var myArray = [1, 2];
var valueToInsert = 1.5;
var indexToInsert = 1;
var deleteCount = 0;
myArray.splice(indexToInsert, deleteCount, valueToInsert);
console.log(JSON.stringify(myArray));
//outputs: [1, 1.5, 2]

2023-05-07

Javascript :: Add an item at the start of an Array

 Two options mostly use:

  • unshift()
  • splice()

Considering splice because it's always used for inserting at any position in the array.

Using unshift:

var myArray = [1, 2];
myArray.unshift(0);
console.log(JSON.stringify(myArray));
//outputs: [0, 1, 2]


Using splice:

var myArray = [1, 2];
var valueToInsert = -1;
var indexToInsert = 0;
var deleteCount = 0;
myArray.splice(indexToInsert, deleteCount, valueToInsert);
console.log(JSON.stringify(myArray));
//outputs: [-1, 1, 2]



Javascript :: comparison operators, using == vs === and != vs !==

Comparison operators are distinguished following simple logic, whether data type is being included or not. Essentially if it is === or !==, ...