diff --git a/src/exercises/1/exercise.js b/src/exercises/1/exercise.js index fcf3805e..3d7b399c 100644 --- a/src/exercises/1/exercise.js +++ b/src/exercises/1/exercise.js @@ -3,7 +3,12 @@ // - Fix the issue. function printOneToTen() { - for (const number = 1; number <= 10; number++) { - console.log(`\n${number}`); + let x = []; + for (let number = 1; number <= 10; number++) { + // Variables defined with const cannot be Reassigned + //console.log(`\n${number}`); + x.push(number); } + return x; } +module.exports = printOneToTen; \ No newline at end of file diff --git a/src/exercises/1/exercise1.test.js b/src/exercises/1/exercise1.test.js new file mode 100644 index 00000000..8f977cfc --- /dev/null +++ b/src/exercises/1/exercise1.test.js @@ -0,0 +1,5 @@ +const printOneToTen = require('./exercise.js'); + +test('print one to ten', () => { + expect(printOneToTen()).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +}); \ No newline at end of file diff --git a/src/exercises/10/exercise.js b/src/exercises/10/exercise.js index 0deb7689..599c5bf4 100644 --- a/src/exercises/10/exercise.js +++ b/src/exercises/10/exercise.js @@ -15,4 +15,8 @@ } ] */ -function findVoteEligibleCandidates(populationList) {} +function findVoteEligibleCandidates(populationList) { + console.log(`${populationList.filter((element) => element.age >= 18).map((element) => element.name)}`); +} + +module.exports = findVoteEligibleCandidates; \ No newline at end of file diff --git a/src/exercises/10/exercise10.test.js b/src/exercises/10/exercise10.test.js new file mode 100644 index 00000000..ee1ca867 --- /dev/null +++ b/src/exercises/10/exercise10.test.js @@ -0,0 +1,9 @@ +const findVoteEligibleCandidates = require('./exercise.js'); + +test('find vote eligible candidates', () => { + const consolepass = jest.spyOn(console, 'log'); + findVoteEligibleCandidates([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]); + expect(consolepass).toHaveBeenCalledWith(`${[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100].filter((element) => element.age >= 18).map((element) => element.name)}`); + findVoteEligibleCandidates([1, 12, 14]); + expect(consolepass).toHaveBeenCalledWith(`${[1, 12, 14].filter((element) => element.age >= 18).map((element) => element.name)}`); +}); \ No newline at end of file diff --git a/src/exercises/11/exercise.js b/src/exercises/11/exercise.js index 0317df9e..dd700279 100644 --- a/src/exercises/11/exercise.js +++ b/src/exercises/11/exercise.js @@ -2,4 +2,6 @@ // - Use Array methods // - Do not use loops -function sumOfArray(inputArray) {} +function sumOfArray(inputArray) { + return `${inputArray.reduce((total, value) => total + value)}`; +} diff --git a/src/exercises/12/exercise.js b/src/exercises/12/exercise.js index dd4df555..270d313a 100644 --- a/src/exercises/12/exercise.js +++ b/src/exercises/12/exercise.js @@ -5,5 +5,10 @@ function errorFunction() { } function errorCaller() { - errorFunction(); -} + try { + errorFunction(); + } + catch (error) { + console.log(error); + } +} \ No newline at end of file diff --git a/src/exercises/13/exercise.js b/src/exercises/13/exercise.js index cb1ceaeb..ef92727b 100644 --- a/src/exercises/13/exercise.js +++ b/src/exercises/13/exercise.js @@ -5,6 +5,14 @@ const employee = { lastName: "Teja", age: 29, id: 1122, - getFullName: function () {}, - checkVoteEligiblity: function () {}, + getFullName: function () { + return this.firstName + " " + this.lastName; + }, + checkVoteEligiblity: function () { + if (this.age >= 18) { + return "Eligible"; + } else { + return "Not Eligible"; + } + }, }; diff --git a/src/exercises/14/exercise.js b/src/exercises/14/exercise.js index 6e47dd07..42dd17cb 100644 --- a/src/exercises/14/exercise.js +++ b/src/exercises/14/exercise.js @@ -8,10 +8,19 @@ const employee = { id: 1122, getFullName: function () { //Copy the solution from the exercise before this. + return this.firstName + " " + this.lastName; }, }; const newEmployee = { firstName: "New", lastName: "Employee", + getFullName: employee.getFullName, + // or in place of getFullName: employee.getFullName, + // you can also write getFullName: employee.getFullName.bind(newEmployee) + /* or in place of getFullName: employee.getFullName, this can also be done + __proto__: employee + */ }; + +//const getFullName = employee.getFullName.call(newEmployee); //This is one of the way it can be done. \ No newline at end of file diff --git a/src/exercises/15/exercise.js b/src/exercises/15/exercise.js index 1c4a7d48..6b03c672 100644 --- a/src/exercises/15/exercise.js +++ b/src/exercises/15/exercise.js @@ -1,10 +1,8 @@ // Convert the below functions into arrow functions. -const greetings = function () { - return "Hello World"; -}; +const greetings = () => "Hello World"; -const isEven = function (num) { +const isEven = (num) => { if (num % 2 == 0) { return true; } else { diff --git a/src/exercises/16/exercise.js b/src/exercises/16/exercise.js index 01561081..a36c20ab 100644 --- a/src/exercises/16/exercise.js +++ b/src/exercises/16/exercise.js @@ -2,6 +2,51 @@ // - Identify the class variables // - Create function for all the possible functionalities. -class Leave { +export class Leave { //... + constructor(leaveType, leaveStartDate, leaveEndDate) { + //... + this._leaveType = leaveType; + this._leaveStartDate = leaveStartDate; + this._leaveEndDate = leaveEndDate; + this._leaveDays = this.calculateLeaveDays(); + } + + get leaveType() { + return this._leaveType; + } + + get leaveStartDate() { + return this._leaveStartDate; + } + + get leaveEndDate() { + return this._leaveEndDate; + } + + set leaveType(leaveType) { + this._leaveType = leaveType; + } + + set leaveStartDate(leaveStartDate) { + this._leaveStartDate = leaveStartDate; + } + + set leaveEndDate(leaveEndDate) { + this._leaveEndDate = leaveEndDate; + } + + calculateLeaveDays() { + //... + return this._leaveEndDate - this._leaveStartDate; + } + + approveLeave() { + //... + if (this._leaveDays > 5) { + console.log("Leave Rejected"); + } else { + console.log("Leave approved"); + } + } } diff --git a/src/exercises/17/exercise.js b/src/exercises/17/exercise.js index f344d030..9a4d5797 100644 --- a/src/exercises/17/exercise.js +++ b/src/exercises/17/exercise.js @@ -1 +1,45 @@ // Extend the Leave class, to create different classes for different Leave types. +import { Leave } from "../16/exercise.js"; +class SickLeave extends Leave { + constructor(leaveStartDate, leaveEndDate) { + super("Sick", leaveStartDate, leaveEndDate); + this.limit = 5; + } + + approveLeave() { + if (this._leaveDays > this.limit) { + console.log("Leave Rejected"); + } else { + console.log("Leave approved"); + } + sickLeavesLeft(this.limit - this._leaveDays); + } + + sickLeavesLeft(limit) { + this.limit = limit; + console.log("Sick leaves left: " + this.limit); + } +} + +class CasualLeave extends Leave { + constructor(leaveStartDate, leaveEndDate) { + super("Casual", leaveStartDate, leaveEndDate); + this.limit = 10; + } + + approveLeave() { + if (this._leaveDays > this.limit) { + console.log("Leave Rejected"); + } else { + console.log("Leave approved"); + } + casuaLeavesLeft(this.limit - this._leaveDays); + } + + casuaLeavesLeft(limit) { + this.limit = limit; + console.log("Casual leaves left: " + this.limit); + } +} + + diff --git a/src/exercises/18/CasualLeaveClass.js b/src/exercises/18/CasualLeaveClass.js new file mode 100644 index 00000000..f7ee7964 --- /dev/null +++ b/src/exercises/18/CasualLeaveClass.js @@ -0,0 +1,21 @@ +import { Leave } from "../16/exercise.js"; +class CasualLeave extends Leave { + constructor(leaveStartDate, leaveEndDate) { + super("Casual", leaveStartDate, leaveEndDate); + this.limit = 10; + } + + approveLeave() { + if (this._leaveDays > this.limit) { + console.log("Leave Rejected"); + } else { + console.log("Leave approved"); + } + casuaLeavesLeft(this.limit - this._leaveDays); + } + + casuaLeavesLeft(limit) { + this.limit = limit; + console.log("Casual leaves left: " + this.limit); + } +} \ No newline at end of file diff --git a/src/exercises/18/SickLeaveClass.js b/src/exercises/18/SickLeaveClass.js new file mode 100644 index 00000000..67595247 --- /dev/null +++ b/src/exercises/18/SickLeaveClass.js @@ -0,0 +1,21 @@ +import { Leave } from "../16/exercise.js"; +class SickLeave extends Leave { + constructor(leaveStartDate, leaveEndDate) { + super("Sick", leaveStartDate, leaveEndDate); + this.limit = 5; + } + + approveLeave() { + if (this._leaveDays > this.limit) { + console.log("Leave Rejected"); + } else { + console.log("Leave approved"); + } + sickLeavesLeft(this.limit - this._leaveDays); + } + + sickLeavesLeft(limit) { + this.limit = limit; + console.log("Sick leaves left: " + this.limit); + } +} \ No newline at end of file diff --git a/src/exercises/18/exercise.js b/src/exercises/18/exercise.js index e0fd230c..21a921a9 100644 --- a/src/exercises/18/exercise.js +++ b/src/exercises/18/exercise.js @@ -1,3 +1,13 @@ // Use Javascript modules to create seperate files for each of the Leave type classes. // - Comment your understanding about import // - Comment your understanding about export + +/****Import*****/ +//import allows us to import the class, functions, variables from other files and use them in the current file. Modules can be imported in two ways based on the type of export. +//The imported values are read-only views of the features that were exported. Similar to const variables, you cannot re-assign the variable that was imported, but you can still modify properties of object values. The value can only be re-assigned by the module exporting it. + +/****export*****/ +//export allows us to export the class, functions, variables to be used in other files. +//There are two types of exports: Named Exports and Default Exports. Named Exports are used when we have multiple things to export from a file. Default Exports are used when we have only one thing to export from a file. + +//you need to include type="module" in the + + + \ No newline at end of file diff --git a/src/exercises/31/exercise.js b/src/exercises/31/exercise.js index 8409e2ba..9a7a976c 100644 --- a/src/exercises/31/exercise.js +++ b/src/exercises/31/exercise.js @@ -11,3 +11,29 @@ // Declaration of translate // function translate(sourceString:string, targetLanguage:string) +class Translator { + static translate(sourceString, targetLanguage) { + // Your code here + const encodedParams = new URLSearchParams(); + encodedParams.append("q", sourceString); + encodedParams.append("target", targetLanguage); + encodedParams.append("source", "en"); + + const options = { + method: 'POST', + headers: { + 'content-type': 'application/x-www-form-urlencoded', + 'Accept-Encoding': 'application/gzip', + 'X-RapidAPI-Key': '26e8a0f98fmshdcd189ad8bdba07p129d5djsnf0c06a093d31', + 'X-RapidAPI-Host': 'google-translate1.p.rapidapi.com' + }, + body: encodedParams + }; + + fetch('https://google-translate1.p.rapidapi.com/language/translate/v2', options) + .then(response => response.json()) + .then(response => console.log(response.data.translations[0].translatedText)) + .catch(err => console.error(err)); + } +} +Translator.translate("Hello", "fr"); diff --git a/src/exercises/32/exercise.js b/src/exercises/32/exercise.js index f990b0a5..135fc840 100644 --- a/src/exercises/32/exercise.js +++ b/src/exercises/32/exercise.js @@ -35,3 +35,38 @@ */ +class Airport { + async getInfoFromIATA(iata) { + // Your code here + const options = { + method: 'GET', + headers: { + 'X-RapidAPI-Key': '26e8a0f98fmshdcd189ad8bdba07p129d5djsnf0c06a093d31', + 'X-RapidAPI-Host': 'airport-info.p.rapidapi.com' + } + }; + const res = await fetch(`https://airport-info.p.rapidapi.com/airport?iata=${iata}`, options); + const data = await res.json(); + return data + } + + async getInfoFromICAO(icao) { + // Your code here + const options = { + method: 'GET', + headers: { + 'X-RapidAPI-Key': '26e8a0f98fmshdcd189ad8bdba07p129d5djsnf0c06a093d31', + 'X-RapidAPI-Host': 'airport-info.p.rapidapi.com' + } + }; + + fetch(`https://airport-info.p.rapidapi.com/airport?icao=${icao}`, options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); + } +} +let temp = new Airport() +temp.getInfoFromIATA("hyd").then(response => console.log(response)) + .catch(err => console.error(err)); +temp.getInfoFromICAO("hyd"); \ No newline at end of file diff --git a/src/exercises/33/exercise.js b/src/exercises/33/exercise.js index 1d66757b..d3e7c199 100644 --- a/src/exercises/33/exercise.js +++ b/src/exercises/33/exercise.js @@ -4,11 +4,11 @@ // - Read : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax // Given an object -// { -// name: "Ravi", -// id : 1122, -// age : 29 -// } +object = { + name: "Ravi", + id: 1122, + age: 29 +} // Create a two variables // 1. name - Its value should be "Ravi" @@ -16,3 +16,7 @@ // id : 1122, // age : 29 // } + +const { name, ...otherProperties } = object; +console.log(name); +console.log(otherProperties); diff --git a/src/exercises/34/exercise.js b/src/exercises/34/exercise.js index c018d0e3..3f1d5eeb 100644 --- a/src/exercises/34/exercise.js +++ b/src/exercises/34/exercise.js @@ -1,10 +1,14 @@ // Default parameters // - Read : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters -function multiply(num1, num2) { +function multiply(num1 = 1, num2 = 1) { return num1 * num2; } +console.log(multiply(2, 3)); +console.log(multiply(4)); +console.log(multiply()); + // multiply(2,3) - returns 6 // multiple(4) - return NaN diff --git a/src/exercises/35/exercise.js b/src/exercises/35/exercise.js index 0b86ce11..33ba81fe 100644 --- a/src/exercises/35/exercise.js +++ b/src/exercises/35/exercise.js @@ -11,9 +11,9 @@ const employeeRavi = { }; function getExperienceOfRavi() { - if (employeeRavi.getExperience) { - console.log(employeeRavi.getExperience()); - } + console.log(employeeRavi.getExperience?.()); } +getExperienceOfRavi(); + // - Modify the above function to use optional chaining. diff --git a/src/exercises/36/exercise.js b/src/exercises/36/exercise.js index 23047b1f..fb8fb462 100644 --- a/src/exercises/36/exercise.js +++ b/src/exercises/36/exercise.js @@ -21,3 +21,14 @@ console.log(A() ?? C() ?? D()); console.log(B() ?? D()); //Comment the outputs below and reason +/* + A was called + C was called + D was called + foo + B was called + false +*/ + +// First A is called and it returns undefined. So the next value is C which returns null. So the next value is D which returns "foo". So the final value is "foo". +// In the second case B is called and it returns false. So it doesn't go to the next value. \ No newline at end of file diff --git a/src/exercises/4/exercise.js b/src/exercises/4/exercise.js index d977ce66..889f2896 100644 --- a/src/exercises/4/exercise.js +++ b/src/exercises/4/exercise.js @@ -3,7 +3,8 @@ // - Comment its uses. function greeting(firstName, lastName) { - let welcomeGreeting; - + let welcomeGreeting = `Hello ${firstName} ${lastName}!`; + //Template literals provide an easy way to interpolate variables and expressions into strings. console.log(welcomeGreeting); } +module.exports = greeting; \ No newline at end of file diff --git a/src/exercises/4/exercise4.test.js b/src/exercises/4/exercise4.test.js new file mode 100644 index 00000000..90bc7a50 --- /dev/null +++ b/src/exercises/4/exercise4.test.js @@ -0,0 +1,7 @@ +const greeting = require('./exercise.js'); + +test('greeting', () => { + const consolepass = jest.spyOn(console, 'log'); + greeting("John", "Doe"); + expect(consolepass).toHaveBeenCalledWith("Hello John Doe!"); +}); \ No newline at end of file diff --git a/src/exercises/5/exercise.js b/src/exercises/5/exercise.js index 14ea345c..c034d9f2 100644 --- a/src/exercises/5/exercise.js +++ b/src/exercises/5/exercise.js @@ -3,8 +3,10 @@ // - Comment the function that you used. function binaryToDecimal(binaryString) { - let decimalValue; + let decimalValue = parseInt(binaryString, 2); + //parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems). console.log( `Decimal for the given binary string ${binaryString} is ${decimalValue}` ); } +module.exports = binaryToDecimal; \ No newline at end of file diff --git a/src/exercises/5/exercise5.test.js b/src/exercises/5/exercise5.test.js new file mode 100644 index 00000000..c1291025 --- /dev/null +++ b/src/exercises/5/exercise5.test.js @@ -0,0 +1,11 @@ +const binaryToDecimal = require('./exercise.js'); + +test('binary to decimal', () => { + const consolepass = jest.spyOn(console, 'log'); + binaryToDecimal(1001); + expect(consolepass).toHaveBeenCalledWith("Decimal for the given binary string 1001 is 9"); + binaryToDecimal(11); + expect(consolepass).toHaveBeenCalledWith("Decimal for the given binary string 11 is 3"); + binaryToDecimal(0); + expect(consolepass).toHaveBeenCalledWith("Decimal for the given binary string 0 is 0"); +}); \ No newline at end of file diff --git a/src/exercises/6/exercise.js b/src/exercises/6/exercise.js index a7149958..ed9024da 100644 --- a/src/exercises/6/exercise.js +++ b/src/exercises/6/exercise.js @@ -2,6 +2,13 @@ // - Also validate the input. Accept the input only if its an array. function findTheLength(inputArray) { - let lengthOfArray; - console.log(`Length of the given input array is ${lengthOfArray}`); + const validate = inputArray instanceof Array; // Array.isArray(inputArray) is another way to validate the input + let lengthOfArray = inputArray.length; + if (validate) { + console.log(`Length of the given input array is ${lengthOfArray}`); + } + else { + console.log("Please enter an array"); + } } +//module.exports = findTheLength; \ No newline at end of file diff --git a/src/exercises/6/exercise6.test.js b/src/exercises/6/exercise6.test.js new file mode 100644 index 00000000..62586ba2 --- /dev/null +++ b/src/exercises/6/exercise6.test.js @@ -0,0 +1,11 @@ +const findTheLength = require('./exercise.js'); + +test('find the length', () => { + const consolepass = jest.spyOn(console, 'log'); + findTheLength("Hello World!"); + expect(consolepass).toHaveBeenCalledWith("Please enter an array"); + findTheLength([]); + expect(consolepass).toHaveBeenCalledWith("Length of the given input array is 0"); + findTheLength([1, 2, 3]); + expect(consolepass).toHaveBeenCalledWith("Length of the given input array is 3"); +}); \ No newline at end of file diff --git a/src/exercises/7/exercise.js b/src/exercises/7/exercise.js index 8c18d178..3c4e4a63 100644 --- a/src/exercises/7/exercise.js +++ b/src/exercises/7/exercise.js @@ -4,4 +4,6 @@ function commaSeparatedString(inputArray) { // Given an Array like ["firstName","lastName"] // Return a comma separated String like "firstName,lastName" + return inputArray.join(","); } +module.exports = commaSeparatedString; \ No newline at end of file diff --git a/src/exercises/7/exercise7.test.js b/src/exercises/7/exercise7.test.js new file mode 100644 index 00000000..9648a20b --- /dev/null +++ b/src/exercises/7/exercise7.test.js @@ -0,0 +1,7 @@ +const commaSeparatedString = require('./exercise.js'); + +test('comma separated string', () => { + expect(commaSeparatedString(["John", "Doe", "Jane", "Doe"])).toEqual("John,Doe,Jane,Doe"); + expect(commaSeparatedString(["John", "Doe"])).toEqual("John,Doe"); + expect(commaSeparatedString(["John"])).toEqual("John"); +}); \ No newline at end of file diff --git a/src/exercises/8/exercise.js b/src/exercises/8/exercise.js index e127b842..d28db3f2 100644 --- a/src/exercises/8/exercise.js +++ b/src/exercises/8/exercise.js @@ -2,4 +2,7 @@ // - Print the given Array. One element in one line // - Use loops -function printArray(inputArray) {} +function printArray(inputArray) { + inputArray.forEach((element) => { console.log(element); }); +} +//module.exports = printArray; \ No newline at end of file diff --git a/src/exercises/8/exercise8.test.js b/src/exercises/8/exercise8.test.js new file mode 100644 index 00000000..90e9754d --- /dev/null +++ b/src/exercises/8/exercise8.test.js @@ -0,0 +1,9 @@ +const printArray = require('./exercise.js'); + +test('print array', () => { + const consolepass = jest.spyOn(console, 'log'); + printArray([1, 2, 3]); + [1, 2, 3].forEach((ele) => { expect(consolepass).toHaveBeenCalledWith(ele) }); + printArray([]); + [].forEach((ele) => { expect(consolepass).toHaveBeenCalledWith(ele) }); +}); \ No newline at end of file diff --git a/src/exercises/9/exercise.js b/src/exercises/9/exercise.js index d63979cf..56ff4e89 100644 --- a/src/exercises/9/exercise.js +++ b/src/exercises/9/exercise.js @@ -3,5 +3,6 @@ // - Do not use loops function multiplyArrayByTwo(inputArray) { - console.log(`Given input array is ${inputArray}`); + console.log(`Given input array is ${inputArray.map((element) => element * 2)}`); } +module.exports = multiplyArrayByTwo; \ No newline at end of file diff --git a/src/exercises/9/exercise9.test.js b/src/exercises/9/exercise9.test.js new file mode 100644 index 00000000..00cf787f --- /dev/null +++ b/src/exercises/9/exercise9.test.js @@ -0,0 +1,9 @@ +const multiplyArrayByTwo = require('./exercise.js'); + +test('multiply array by two', () => { + const consolepass = jest.spyOn(console, 'log'); + multiplyArrayByTwo([1, 2, 3]); + expect(consolepass).toHaveBeenCalledWith(`Given input array is ${[1, 2, 3].map((element) => element * 2)}`); + multiplyArrayByTwo([]); + expect(consolepass).toHaveBeenCalledWith(`Given input array is ${[].map((element) => element * 2)}`); +}); \ No newline at end of file