willwm's notebooks

  • FizzBuzz - /willwm/fizzbuzz
    Last edited 3 months ago
    // Based on FizzBuzz description from: https://en.wikipedia.org/wiki/Fizz_buzz function fizzbuzz(val) { let output = ''; if (val % 3 === 0) { output += 'Fizz'; } if (val % 5 === 0) { output += 'Buzz'; } if (output === '') { output = `${val}`; } return output; } function runner(iterations) { for (i = 1; i <= iterations; i++) { const result = fizzbuzz(i); console.log(`${i}: ${result}`); } } runner(100);
  • ES2015 Class Example - /willwm/es2015-class-example
    Last edited 5 years 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 6 years 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');