Skip to content

Instantly share code, notes, and snippets.

//Example 4
const arr = ['a', 'b'];
// Validate that an array literal will have the 'Array.prototype' object
// as its immediate parent in the prototype chain which in turn will have
// as its prototype 'Object.prototype' which is the last link in the chain
console.log(arr.__proto__ === Array.prototype); // true
console.log(Array.prototype.__proto__ === Object.prototype); // true
// Validate that 'Object.prototype' is the last link in the prototype chain as
//Example 3
const arr = ['a', 'b'];
// Arrays have a type of 'object' and in addition to the
// explicitly declared properties and values, it can also access
// built-in methods through inheritance along its prototype chain
console.log(typeof arr); // 'object'
console.log(arr[0]); // 'a'
console.log(arr[1]); // 'b'
console.log(arr.join('')); // 'ab'
@nirabpant
nirabpant / JS_OOP_example2.js
Last active October 26, 2021 04:35
Testing the properties of a function literal
// Example 2
// create new function 'func'
const func = function() {
console.log('hello world!');
};
// Validate that the [[Prototype]] of the function object 'func' is 'Function.prototype'
// which is the base object from which all other functions inherit. The 'constructor'
// property of 'func' simply points back to the 'Function' constructor.
console.log(func.__proto__ === Function.prototype); // true
// Example 1
const obj = {
color: "blue",
amount: 100,
};
// Check the type of the object literal 'obj'
console.log(typeof obj); // 'object'
// Verify that 'obj' has the properties we expect it to have
@nirabpant
nirabpant / brute-force.js
Last active October 10, 2021 04:48
Brute-force approach
function nextBiggerNum(inputNum) {
let newNum = inputNum + 1;
let initLen = String(inputNum).length;
while (String(newNum).length <= initLen) {
if (sameDigits(inputNum, newNum)) {
return newNum;
}
newNum += 1;
}
@nirabpant
nirabpant / recursive.js
Last active October 10, 2021 04:43
Recursive Code Implementation
function nextBiggerNum(num) {
let validNumsArr = getValidNums(num);
let validNumsSorted = getSortedUniqueNums(validNumsArr);
let numIdx = validNumsSorted.indexOf(String(num));
// check if largest possible num
if (numIdx === validNumsSorted.length - 1) return -1;
// if not largest, return next bigger num