willwm's notebooks

  • ES2015 Class Example - /willwm/es2015-class-example
    Last edited 2 months ago
    class Elevator { constructor(name, floorNumber, status) { this.name = name; this.floorNumber = floorNumber; this.status = status; } get info() { return `Elevator ${this.name} - Floor ${this.floorNumber}, Status ${this.status}`; } get floor() { return this.floorNumber; } moveUp() { this.floorNumber++; } moveDown() { this.floorNumber--; } moveToFloor(floorNumber) { this.floorNumber = floorNumber; } } const e1 = new Elevator("e1", 1, "operating"); const e2 = new Elevator("e2", 5, "operating"); console.log(e1.info); console.log(e2.info); e1.moveUp(); console.log(e1.info); e2.moveDown(); console.log(e2.info); e1.moveToFloor(20); console.log(e1.floor);
  • String.localeCompare Example - /willwm/string-localecompare-example
    Last edited a year ago
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare function areEqualIgnoreCase(str1, str2) { if (!str1 || !str2) { return false; } const result = str1.localeCompare(str2, undefined, { sensitivity: 'base' }); return (result === 0); } function runTestCase(str1, str2) { const areEqual = areEqualIgnoreCase(str1, str2); console.log(`Are ${str1} and ${str2} equal (ignoring case)? ${areEqual}`); } runTestCase('foo', 'bar'); runTestCase('foo', 'foo'); runTestCase('foo', 'FoO'); runTestCase('bar', 'BAR');