This file contains hidden or 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
| function getLuckyNumber(){ | |
| const userInput = prompt("Enter your lucky number"); | |
| const parsedValue = parseInt(userInput); | |
| if(isNan(parsedValue) || parsedValue <=0){ | |
| throw { message: "Invalid user input"}; | |
| } | |
| return parsedValue; | |
| } |
This file contains hidden or 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
| class Error { | |
| constructor(message) { | |
| this.message = message; | |
| this.name = "Error"; | |
| this.stack = <call stack>; | |
| } | |
| } |
This file contains hidden or 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
| class ValidationError extends Error { | |
| constructor(message) { | |
| super(message); | |
| this.name = "ValidationError"; | |
| } | |
| } |
This file contains hidden or 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
| class PropertyRequiredError extends ValidationError { | |
| constructor(property) { | |
| super("No property: " + property); | |
| this.name = "PropertyRequiredError"; | |
| this.property = property; | |
| } | |
| } |
This file contains hidden or 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
| try { | |
| luckyNumber = getLuckyNumber(); | |
| } catch(error){ | |
| console.log(error); | |
| luckyNumber = Math.floor(Math.random() * 101); | |
| alert("You entered something wrong, a random value will be used."); | |
| } |
This file contains hidden or 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
| function createCar(json) { | |
| let car = JSON.parse(json); | |
| if (!car.age) { | |
| throw new PropertyRequiredError("age"); | |
| } | |
| if (!car.color) { | |
| throw new PropertyRequiredError("color"); | |
| } | |
| if (!car.brandName) { |
This file contains hidden or 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
| try { | |
| let car = createCar ('{ "color": black-yellow}'); | |
| } catch (err) { | |
| if (err instanceof ValidationError) { | |
| alert("Invalid data: " + err.message); | |
| alert(err.name); // PropertyRequiredError | |
| alert(err.property); // name | |
| } else { | |
| throw err; | |
| } |
This file contains hidden or 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
| let pipeline = [increment, double, decrement]; | |
| const result = pipeline.reduce(function(total, func) { | |
| return func(total); | |
| }, 8); | |
| // result: 17 |
This file contains hidden or 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
| const apples = ['green', 'red', 'red', 'yellow', 'red', 'yellow', 'green', 'green']; | |
| var appleMap = apples.reduce((prev, apple) => { | |
| if (prev[apple] >= 1) prev[apple]++; | |
| else prev[apple] = 1; | |
| return prev; | |
| }, {}); | |
| console.log(appleMap); | |
| // result: {green: 3, red: 3, yellow: 2} |
This file contains hidden or 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
| const apples = ['green', 'red', 'red', 'yellow', 'red', 'yellow', 'green', 'green']; | |
| var appleMap = apples.reduce((prev, apple) => { | |
| if (prev[apple] >= 1) prev[apple]++; | |
| else prev[apple] = 1; | |
| return prev; | |
| }, {}); | |
| console.log(appleMap); | |
| // result: {green: 3, red: 3, yellow: 2} |
OlderNewer