# ERROR Handling

assignment solution

<table><tbody><tr><td colspan="1" rowspan="1"><p>// 1. Type Conversion.<br><br>// Write a function called convertToNumber that takes a string as an argument and returns the equivalent number. If the string cannot be converted to a number, the function should return the string "Invalid number". Use error handling in javascript to achieve this output.<br><br>function convertToNumber(str) {<br>&nbsp; try {<br>&nbsp;&nbsp;&nbsp; const num = Number(str);<br>&nbsp;&nbsp;&nbsp; if (Number.isNaN(num)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new Error("Invalid number");<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return num;<br>&nbsp; } catch (error) {<br>&nbsp;&nbsp;&nbsp; return error.message;<br>&nbsp; }<br>}<br><br>console.log(convertToNumber("123"));<br>console.log(convertToNumber("abc"));</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>// 2. Building Robust Functions in JavaScript<br><br>// Create a function called getPerson that takes an object as a parameter representing a person's name and age. The function should return the person's name and age as a string in the format "Name: &lt;name&gt;, Age: &lt;age&gt;". However, if the parameter is not a valid object with the properties "name" and "age", the function should throw an error with the message "Invalid parameter type". Use try-catch to handle this error and return the error message if it occurs.<br><br>function getPerson(person) {<br>&nbsp; try {<br>&nbsp;&nbsp;&nbsp; if (typeof person !== "object" || !<a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none">person.name</a> || !person.age) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new Error("Invalid parameter type");<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return <code>Name: ${</code><a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none"><code>person.name</code></a><code>}, Age: ${person.age}</code>;<br>&nbsp; } catch (error) {<br>&nbsp;&nbsp;&nbsp; return error.message;<br>&nbsp; }<br>}<br><br>// Expected Output<br>console.log(getPerson({ name: "Mithun", age: 20 })); // Name: Mithun, Age: 20<br>console.log(getPerson({ name: "Mithun" })); // Invalid parameter type<br>console.log(getPerson(["name", "Mithun"])); // Invalid parameter type</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>// 3. Car Description Class.<br><br>// Create a class called Car with three properties: company, model, and year. The class should have a method called getDescription that returns a string in the format "This is a &lt;year&gt; &lt;company&gt; &lt;model&gt;". Instantiate an instance of the Car class and call the getDescription method.<br><br>// Expected Output<br><br>// console.log(myCar.getDescription());<br>// Output: This is a 2022 Skoda Rapid.<br><br>class Car {<br>&nbsp; constructor(company, model, year) {<br>&nbsp;&nbsp;&nbsp; <a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none">this.company</a> = company;<br>&nbsp;&nbsp;&nbsp; this.model = model;<br>&nbsp;&nbsp;&nbsp; this.year = year;<br>&nbsp; }<br><br>&nbsp; getDescription() {<br>&nbsp;&nbsp;&nbsp; return <code>This is a ${this.year} ${</code><a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none"><code>this.company</code></a><code>} ${this.model}.</code>;<br>&nbsp; }<br>}<br><br>const myCar = new Car("Skoda", "Rapid", 2022);<br><br>console.log(myCar.getDescription()); // Output: This is a 2022 Skoda Rapid.</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>/*<br>4. Employee Class Challenge.<br><br>Create a class called Employee with three properties: name, position, and salary. The class should have a method called getSalary that returns the employee's salary. Instantiate an instance of the Employee class and call the getSalary method.<br><br>Expected Output:<br>console.log(employee1.getSalary()); // Output: 80000<br>*/<br><br>class Employee {<br>&nbsp; constructor(name, position, salary) {<br>&nbsp;&nbsp;&nbsp; <a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none">this.name</a> = name;<br>&nbsp;&nbsp;&nbsp; this.position = position;<br>&nbsp;&nbsp;&nbsp; this.salary = salary;<br>&nbsp; }<br><br>&nbsp; getSalary() {<br>&nbsp;&nbsp;&nbsp; return this.salary;<br>&nbsp; }<br>}<br><br>const employee1 = new Employee("Prabir Kumar", "Software Engineer", 80000);<br><br>// Expected Output<br>console.log(employee1.getSalary()); // Output: 80000</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>/*<br><br>5. Implementing a Person Class with Default Values<br><br>Create a class called Person with two properties: name and age. The class should have a method called getDetails that returns a string in the format "Name: &lt;name&gt;, Age: &lt;age&gt;". Use default parameters in the constructor to set the values of name and age to "Unknown" and 0 if they are not provided.<br><br>Expected Output<br><br>const person1 = new Person("Mithun", 20);<br>console.log(person1.getDetails()); // Output: "Name: Mithun, Age: 20"<br><br>const person2 = new Person();<br>console.log(person2.getDetails()); // Output: "Name: Unknown, Age: 0"<br><br>*/<br><br>class Person {<br>&nbsp; constructor(name = "Unknown", age = 0) {<br>&nbsp;&nbsp;&nbsp; <a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none">this.name</a> = name;<br>&nbsp;&nbsp;&nbsp; this.age = age;<br>&nbsp; }<br><br>&nbsp; getDetails() {<br>&nbsp;&nbsp;&nbsp; return <code>Name: ${</code><a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none"><code>this.name</code></a><code>}, Age: ${this.age}</code>;<br>&nbsp; }<br>}<br><br>// Expected Output<br><br>const person1 = new Person("Mithun", 20);<br>console.log(person1.getDetails()); // Output: "Name: Mithun, Age: 20"<br><br>const person2 = new Person();<br>console.log(person2.getDetails()); // Output: "Name: Unknown, Age: 0"</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>/*<br><br>6. Using Static Method to Add Two Numbers with Calculator Class<br><br>Create a class called Calculator with a static method called add. The add method should take two numbers as arguments and return their sum. Instantiate the Calculator class and call the add method.<br><br>Expected Output:<br><br>console.log(result); // Output: 15<br><br>*/<br><br>class Calculator {<br>&nbsp; static add(num1, num2) {<br>&nbsp;&nbsp;&nbsp; return num1 + num2;<br>&nbsp; }<br>}<br><br>const result = Calculator.add(5, 10);<br><br>// Expected Output:<br>console.log(result); // Output: 15</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>/*<br><br>7. Password Checker.<br><br>Create a class called User with properties username and password. Implement a getter method for password that returns the password with all characters replaced by asterisks. Implement a setter method for password that checks if the new password is at least 8 characters long and contains at least one number and one uppercase letter. If the password is valid, set the new password. If not, log an error message.<br><br>Expected output:<br>const user = new User("johndoe", "Password123");<br>console.log(user.getPassword()); // ***********<br><br>user.setPassword("myPassword"); // Error: Password must be at least 8 characters long and contain at least one number and one uppercase letter.<br><br>user.setPassword("MyPassword"); // Error: Password must be at least 8 characters long and contain at least one number and one uppercase letter.<br><br>user.setPassword("Mypassword123");<br>console.log(user.getPassword()); // *************<br><br><br><br>*/<br><br></p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>// 8. Adding a Method to a Prototype.<br><br>// Create a prototype object called Student with a property name. Add a method called printDetails to the prototype that logs the string "Hello, my name is {name}" to the console. Instantiate a Student object with the name "Mithun" and call the printDetails method.<br><br>// Expected Output<br><br>// const student = new Student("Mithun");<br>// student.printDetails(); // "Hello, the student is Mithun"<br><br>function Student(name) {<br>&nbsp; <a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none">this.name</a> = name;<br>}<br><br>Student.prototype.printDetails = function () {<br>&nbsp; console.log(`Hello, my name is ${<a target="_blank" rel="noopener noreferrer nofollow" href="file:///C:/Users/Afroj%20Mansoori/AppData/Local/Packages/oice_16_974fa576_32c1d314_67a/AC/Temp/msohtmlclip1/01/clip_filelist.xml" style="pointer-events: none">this.name</a>}`);<br>};<br><br>// Expected Output<br><br>const student = new Student("Mithun");<br>student.printDetails(); // "Hello, the student is Mithun"</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>/*<br><br>9. Check the presence using closures.<br><br>Create a numberChecker function that takes an array of numbers as an argument and returns a function. The returned function should take another number as an argument and return true if the number is in the array, and false otherwise.<br><br><br>*/<br><br>function numberChecker(numbers) {<br>&nbsp; return function (num) {<br>&nbsp;&nbsp;&nbsp; return numbers.includes(num);<br>&nbsp; };<br>}<br><br>// Expected Result:<br>const arr = [1, 2, 3, 4, 5];<br>const checkNum = numberChecker(arr);<br><br>console.log(checkNum(3)); // true<br>console.log(checkNum(6)); // false</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p>/*<br><br>10. Filter by Category.<br><br>Write a function that takes an array of products and returns a function that filters the array by a given product category. The function must filter an eCommerce products array by a specific category. The closure filters products using the filter() method. Finally, it returns a new array containing only the products with the same category as the input.<br><br><br>*/<br><br>function filterByCategory(products) {<br>&nbsp; return function (category) {<br>&nbsp;&nbsp;&nbsp; return products.filter(function (product) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return product.category === category;<br>&nbsp;&nbsp;&nbsp; });<br>&nbsp; };<br>}<br><br>// Expected Output<br><br>var products = [<br>&nbsp; { name: "Shirt", category: "Clothing" },<br>&nbsp; { name: "Pants", category: "Clothing" },<br>&nbsp; { name: "Hat", category: "Accessories" },<br>&nbsp; { name: "Sunglasses", category: "Accessories" },<br>];<br><br>var clothingProducts = filterByCategory(products)("Clothing");<br><br>console.log(clothingProducts);<br>// Output: [{name: "Shirt", category: "Clothing"}, {name: "Pants", category: "Clothing"}]</p></td></tr></tbody></table>
