Created
March 9, 2020 23:16
-
-
Save sagrath23/026e90f4916bb929ec6d5854974fd1b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Given the following code, determine the output of object.hi | |
let object = { | |
some: "foo", | |
bar: "some", | |
hi: () => { | |
console.log(`${this.some}, ${this.bar}`); | |
} | |
}; | |
object.hi(); // should output "undefined, undefined" because scope of this pointer in arrow function is different from class | |
//fix it! | |
object = { | |
some: "foo", | |
bar: "some", | |
hi: function() { | |
console.log(`${this.some}, ${this.bar}`); | |
} // just change arrow function for named function | |
}; | |
object.hi(); | |
// 2. given the following code | |
(function() { | |
console.log(1); | |
setTimeout(() => { | |
console.log(2); | |
}, 1000); | |
setTimeout(() => { | |
console.log(3); | |
}, 0); | |
console.log(4); | |
})(); | |
// determine the order of output of console.log statements: 1, 4, 3, 2 | |
// because setTimeout add functions in a queue to be added in event loop | |
// try print 3 before 4 | |
(function() { | |
console.log(1); | |
setTimeout(() => { | |
console.log(2); | |
}, 1000); | |
setTimeout(() => { | |
console.log(3); | |
}, 0); | |
setTimeout(() => { | |
console.log(4); | |
}, 0); | |
})(); // ahould print 1, 3, 4, 2 | |
// 3. given the following class, write some test cases for MyMath class | |
import { isZero } from "is-zero-library"; | |
class MyMath { | |
add(...params) { | |
return params.reduce((acm, current) => acm + current, 0); | |
} | |
divide(a, b) { | |
if (b === null || b === undefined) { | |
throw new Error("Should pass a value to b parameter"); | |
} | |
if (isZero(b)) { | |
throw new Error("b parameter can't be zero"); | |
} | |
return a / b; | |
} | |
} | |
// in add function, you can test: | |
// - if no params are provided, should return zero | |
// - if only one parameter is provided, should return the same value | |
// - some edge case like: if NaN is provided as a param, should return NaN | |
// in divide function you can test: | |
// - if b is null, undefined or zero, shoud throw an error | |
// - if a & b params are good, should return the division | |
// - some edge case like if a string is provided | |
// 4. Write a function that handle both calls | |
console.log(sum(2, 1)); // return 3 | |
console.log(sum(2)(1)); // return 3 | |
// in this point, you should try to create a function that can handle both: curried (in a very basic way) and normal invoke | |
const sum = (a, b) => { | |
if (b) { | |
return a + b; | |
} | |
return b => a + b; | |
}; | |
// 5. Given the following code, try to refactor using promises | |
const fs = require('fs'); | |
fs.readFile("someFile.txt", function(err, data) { | |
if (err) { | |
throw new Error(); | |
} else { | |
fs.readdir("someDirectory", function(err, files) { | |
if (err) { | |
throw new Error(); | |
} else { | |
fs.mkdir("someOtherDirectory", function(err) { | |
if (err) { | |
throw new Error(); | |
} else { | |
console.log(data); | |
console.log(files); | |
console.log("someDirectory created"); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
// aux function to promisify passed function | |
const promisifyFs = (func) => (...params) => new Promise((resolve, reject) => { | |
try { | |
func(...params, (err, data) => { | |
if (err) { | |
reject(err); | |
} | |
resolve(data); | |
}); | |
} catch(err) { | |
reject(err); | |
} | |
}); | |
// with promises | |
// promises | |
const procedurePromised = () => { | |
const promisifyRead = promisifyFs(fs.readFile); | |
const promisifyReadDir = promisifyFs(fs.readdir); | |
const promisifyCreateDir = promisifyFs(fs.mkdir); | |
const handleError = (err) => { console.log('error'); }; | |
promisifyRead('someFile.txt') | |
.then((fileData) => { | |
console.log(fileData, 'file'); | |
promisifyReadDir('someDir') | |
.then((dirData) => { | |
console.log(dirData, 'dir'); | |
promisifyCreateDir('someOtherDir') | |
.then(() => { | |
console.log('someOtherDir created'); | |
}) | |
.catch(handleError); | |
}) | |
.catch(handleError) | |
}) | |
.catch(handleError) | |
}; | |
procedurePromised(); | |
// async / await (I didn't ask if I can use async/ await to refactor the code, my mistake) | |
const procedureAsync = async () => { | |
const promisifyRead = promisifyFs(fs.readFile); | |
const promisifyReadDir = promisifyFs(fs.readdir); | |
const promisifyCreateDir = promisifyFs(fs.mkdir); | |
try { | |
const fileData = await promisifyRead('someFile.txt'); | |
console.log(fileData, 'file'); | |
const dirData = await promisifyReadDir('someDir'); | |
console.log(dirData); | |
await promisifyCreateDir('someOtherDirectory'); | |
console.log('someOtherDirectory created'); | |
} catch(error) { | |
throw new Error(error); | |
} | |
}; | |
procedureAsync(); | |
// 6. & 7. Given an array of integers, find if the array contains any duplicates, and explain the complexity of the algorithm. | |
// I made this very quickly, but this can be done by counting algorithm | |
const containsDuplicate = (array) => { | |
const sortedArray = array.sort((a, b) => a - b); // sort the array is O(n*log(n)) | |
let isSomeValueDuplicated = false; | |
for (let i = 0; i < sortedArray.length - 1; i++) { // find the duplicate element is O(n) | |
if(sortedArray[i] === sortedArray[i + 1]) { | |
isSomeValueDuplicated = true; | |
break; | |
} | |
} | |
return isSomeValueDuplicated; | |
}; | |
// tooking the worst complexity of both loops, the algorithm has a complexity of O(n*log(n)) | |
console.log(containsDuplicate([1,2,3,1])) // true | |
console.log(containsDuplicate([1,2,3,4])) // false | |
console.log(containsDuplicate([1,1,1,3,3,4,3,2,4,2])) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment