diff --git a/src/exercises/1/exercise.js b/src/exercises/1/exercise.js index fcf3805e..54234d4a 100644 --- a/src/exercises/1/exercise.js +++ b/src/exercises/1/exercise.js @@ -3,7 +3,14 @@ // - Fix the issue. function printOneToTen() { - for (const number = 1; number <= 10; number++) { + // Error : we can't change const variable in js + // for (const number = 1; number <= 10; number++) { + + // Fix: change const to let + for (let number = 1; number <= 10; number++) { console.log(`\n${number}`); } } + +// we need to call the function to get the Output. +printOneToTen() \ No newline at end of file diff --git a/src/exercises/10/exercise.js b/src/exercises/10/exercise.js index 0deb7689..277bc6c3 100644 --- a/src/exercises/10/exercise.js +++ b/src/exercises/10/exercise.js @@ -15,4 +15,29 @@ } ] */ -function findVoteEligibleCandidates(populationList) {} +function findVoteEligibleCandidates(populationList) { + console.log(populationList.filter((entry) => entry.age >= 18).map((entry => entry.name))) + + // it can also be done as follow: + // console.log(populationList.forEach((entry) => { + // entry.age >= 18 ? console.log(entry.name) : null + // })) + + // NOTE: filter and map returns the list. whereas, forEach doesn't return anything. + // if we need to save the result we can use filter/map not foreach +} + +findVoteEligibleCandidates([ + { + name:"Ravi", + age:28 + }, + { + name:"Teja", + age:18 + }, + { + name:"Spider-man", + age:17 + } +]) diff --git a/src/exercises/11/exercise.js b/src/exercises/11/exercise.js index 0317df9e..14fda415 100644 --- a/src/exercises/11/exercise.js +++ b/src/exercises/11/exercise.js @@ -2,4 +2,12 @@ // - Use Array methods // - Do not use loops -function sumOfArray(inputArray) {} +function sumOfArray(inputArray) { + let sum = 0; + inputArray.forEach(element => { + sum += element; + }); + console.log(sum); +} + +sumOfArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); \ No newline at end of file diff --git a/src/exercises/12/exercise.js b/src/exercises/12/exercise.js index dd4df555..0440e196 100644 --- a/src/exercises/12/exercise.js +++ b/src/exercises/12/exercise.js @@ -5,5 +5,12 @@ function errorFunction() { } function errorCaller() { - errorFunction(); + try { + errorFunction(); + } + catch(e) { + console.log(e); + } } + +errorCaller(); \ No newline at end of file diff --git a/src/exercises/13/exercise.js b/src/exercises/13/exercise.js index cb1ceaeb..9cbe6fb9 100644 --- a/src/exercises/13/exercise.js +++ b/src/exercises/13/exercise.js @@ -5,6 +5,15 @@ const employee = { lastName: "Teja", age: 29, id: 1122, - getFullName: function () {}, - checkVoteEligiblity: function () {}, + getFullName: function () { + return `${this.firstName} ${this.lastName}`; + }, + checkVoteEligiblity: function () { + return this.age >= 18; + }, }; + + + +console.log(employee.getFullName()); +console.log(employee.checkVoteEligiblity()); \ No newline at end of file diff --git a/src/exercises/14/exercise.js b/src/exercises/14/exercise.js index 6e47dd07..fd72eec8 100644 --- a/src/exercises/14/exercise.js +++ b/src/exercises/14/exercise.js @@ -1,5 +1,5 @@ // Get the full name of the new employee. See how you can execute the function "getFullName" on newEmployee object. -// - Comment your findings +// // - Comment your findings const employee = { firstName: "Ravi", @@ -7,7 +7,13 @@ const employee = { age: 29, id: 1122, getFullName: function () { - //Copy the solution from the exercise before this. + //Copy the solution from the exercise before this. + if(arguments.length > 0) { + return `${arguments[0].firstName} ${arguments[0].lastName}`; + } + else { + return `${this.firstName} ${this.lastName}`; + } }, }; @@ -15,3 +21,6 @@ const newEmployee = { firstName: "New", lastName: "Employee", }; + +console.log(employee.getFullName()) +console.log(employee.getFullName(newEmployee)) \ No newline at end of file diff --git a/src/exercises/15/exercise.js b/src/exercises/15/exercise.js index 1c4a7d48..f4bff5e8 100644 --- a/src/exercises/15/exercise.js +++ b/src/exercises/15/exercise.js @@ -1,13 +1,16 @@ // Convert the below functions into arrow functions. -const greetings = function () { +const greetings = () => { return "Hello World"; }; -const isEven = function (num) { +const isEven = (num) => { if (num % 2 == 0) { return true; } else { return false; } }; + +console.log(greetings) +console.log(isEven) \ No newline at end of file diff --git a/src/exercises/16/exercise.js b/src/exercises/16/exercise.js index 01561081..e5502024 100644 --- a/src/exercises/16/exercise.js +++ b/src/exercises/16/exercise.js @@ -2,6 +2,40 @@ // - Identify the class variables // - Create function for all the possible functionalities. -class Leave { +// exporting class to be able to import in next exercise. +export class Leave { //... + constructor(name, reason, startDate, endDate) { + this.name = name; + this.reason = reason; + this.startDate = startDate; + this.endDate = endDate ? endDate : startDate; + this.status = false; + } + + showLeaveDetails() { + console.log(`---- ` + (this.status ? 'granted' : 'not yet granted') + ` ----`) + if(this.startDate === this.endDate) { + console.log(`${this.name} kept leave on ${this.startDate} for ${this.reason}.\n`); + } + else { + console.log(`${this.name} kept leave from ${this.startDate} to ${this.endDate} for ${this.reason}\n`); + } + + } + + grantLeave = function(){ + this.status = true; + console.log(`GRANTED LEAVE TO ${this.name.toUpperCase()}\n`); + } } + +let johnLeave = new Leave("John", "marriage", "17/11/2022", "20/11/2022"); +let raniLeave = new Leave("Rani", "anniversary", "18/11/2022"); + +johnLeave.showLeaveDetails(); +raniLeave.showLeaveDetails(); + +johnLeave.grantLeave(); + +johnLeave.showLeaveDetails(); diff --git a/src/exercises/17/exercise.js b/src/exercises/17/exercise.js index f344d030..4b1c615a 100644 --- a/src/exercises/17/exercise.js +++ b/src/exercises/17/exercise.js @@ -1 +1,24 @@ // Extend the Leave class, to create different classes for different Leave types. + +// imports Leave class from last exercise +import { Leave } from '../16/exercise' + +class CasualLeave extends Leave { + constructor(name, startDate, endDate) { + super(name, "Holiday", startDate, endDate) + } +} + + +class SickLeave extends Leave { + constructor(name, startDate, endDate) { + super(name, "sick", startDate, endDate) + } +} + + +let johnLeave = new CasualLeave("John", "17/11/2022", "20/11/2022"); +let raniLeave = new SickLeave("Rani", "18/11/2022"); + +johnLeave.showLeaveDetails(); +raniLeave.showLeaveDetails(); diff --git a/src/exercises/17/index.html b/src/exercises/17/index.html new file mode 100644 index 00000000..6f11442c --- /dev/null +++ b/src/exercises/17/index.html @@ -0,0 +1,11 @@ + + + + + +

Hello

+ + + + + \ No newline at end of file diff --git a/src/exercises/18/exercise.js b/src/exercises/18/exercise.js index e0fd230c..a9087184 100644 --- a/src/exercises/18/exercise.js +++ b/src/exercises/18/exercise.js @@ -1,3 +1,33 @@ // 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/EXPORT :: + * + * to use js import/export we need to make scripts as modules + * for this we can either use package.json({type: module}) or a HTML file(include scripts and define type="module") to define the type as 'module' + * we can export and import - functions, classes, constants etc... + * Import & Export (using index.html) + * we can do a _simple export_ lets say, + * - export class ClassName {...} + * then we can import it as + * - import { ClassName } from '../fileDirectory' + * + * for example, + * Utils.js + * export function fun1{...} + * export function fun2{...} + * Diff.js + * import {fun1, fun2} from './Utils.js' + * + * + * Or we can do default export like + * - export default class ClassName {...} + * - import ClassName from './path' + * + * Import & Export (using package.json ES-6 modules) + * module.export = { ...list } + * require('../path') -> this will return so we need to save it in a var inorder to use it( const importName = require('../path') ) + */ \ No newline at end of file diff --git a/src/exercises/19/exercise.js b/src/exercises/19/exercise.js index ec707066..e91fbac8 100644 --- a/src/exercises/19/exercise.js +++ b/src/exercises/19/exercise.js @@ -1,3 +1,14 @@ // Comment below what did you understand from the previous 3 exercises about // - prototype // - __proto__ + +/** + * for every class Prototype is a default entry we have + * its like a blueprint of the object + * and __proto__ gives the prototype of the prototype of object + * we can add methods to prototype as follows + * - Book.prototype.getDetails = function() { ... } + * when we console.log any object of Book class it has prototype(even if the object is empty i.e {}) + * and we have this new getDetails function in Book.prototype + * These have Object methods like constructor, hasOwnProperty etc... along with getter and setter. + */ diff --git a/src/exercises/2/exercise.js b/src/exercises/2/exercise.js index a9a49c16..26e18512 100644 --- a/src/exercises/2/exercise.js +++ b/src/exercises/2/exercise.js @@ -3,11 +3,18 @@ // - Fix the issue. function divideTenByNumber(number) { + // Error: result is inside the local space of if and else blocks. So, scope of result is limited to only those blocks. + // Fix: move result above condition statement + + let result; if (number != 0) { - let result = 10 / number; + result = 10 / number; } else { - let result = "Indeterminate"; + result = "Indeterminate"; } console.log(`Result after dividing 10 by ${number} is ${result}`); } + + +divideTenByNumber(0); \ No newline at end of file diff --git a/src/exercises/20/exercise.js b/src/exercises/20/exercise.js index 9a30e605..39c0b5f7 100644 --- a/src/exercises/20/exercise.js +++ b/src/exercises/20/exercise.js @@ -1,3 +1,18 @@ // What is debugger; // - Comment your understanding of it. // - Paste the link of the MDN article here. + + +/** + * Debugger is a keyword in js + * - debugger + * it invokes all the debugging functionalities if present. + * we can use it like a breakpoint to stop the execution at the given point. + * and if any debugger methods are available it runs those functions and then it continues execution + * if no debugger functions are available then it has no effect. + * this helps to find particularly in which statement does the bug is appearing. + * + * MDN link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger + * W3Schools: https://www.w3schools.com/js/js_debugging.asp + * + */ \ No newline at end of file diff --git a/src/exercises/21/exercise.js b/src/exercises/21/exercise.js index 4fe9d13e..f738e538 100644 --- a/src/exercises/21/exercise.js +++ b/src/exercises/21/exercise.js @@ -1 +1,32 @@ // What are the different levels of logs and how do we print them to the console? + +/** + * Different levels of logs are, + * TRACE: trace gives full visibility of what is happening inside the application + * - console.trace('hey') + * DEBUG: handles logging of information that needed to be diagnosed. + * - console.debug('hey') + * INFO: it is just informative. it can be used to give information about the steps that passed smoothly may be in an algorithm. + * - console.info('hey') + * WARN: used to warn about unexpected scenarios that may occurs without actually intrupting the controlflow of the program + * - console.warn('hey') + * ERROR: used to give error messages that oocured + * - console.error('hey') + * FATAL: it gives the critical information like operation fail which is required to move forward ( unable to connect server ) + * - console.fatal('hey') + * + * the one that is widely used is console.log() -> used to print info about anything that occurs in our program. can be used for all levels of logs. + */ + + + + console.trace("Hey!"); + console.debug("Hey!"); + console.info("Hey!"); + console.warn("Hey!"); + console.error("Hey!"); + console.fatal("Hey!"); + + + console.log("Hey!") + \ No newline at end of file diff --git a/src/exercises/22/Screenshot_1.png b/src/exercises/22/Screenshot_1.png new file mode 100644 index 00000000..691b559a Binary files /dev/null and b/src/exercises/22/Screenshot_1.png differ diff --git a/src/exercises/22/Screenshot_2.png b/src/exercises/22/Screenshot_2.png new file mode 100644 index 00000000..b2dd48c2 Binary files /dev/null and b/src/exercises/22/Screenshot_2.png differ diff --git a/src/exercises/22/Screenshot_3.png b/src/exercises/22/Screenshot_3.png new file mode 100644 index 00000000..3d40f5ab Binary files /dev/null and b/src/exercises/22/Screenshot_3.png differ diff --git a/src/exercises/22/Screenshot_4.png b/src/exercises/22/Screenshot_4.png new file mode 100644 index 00000000..3450b757 Binary files /dev/null and b/src/exercises/22/Screenshot_4.png differ diff --git a/src/exercises/22/Screenshot_5.png b/src/exercises/22/Screenshot_5.png new file mode 100644 index 00000000..44ce963a Binary files /dev/null and b/src/exercises/22/Screenshot_5.png differ diff --git a/src/exercises/22/Screenshot_6.png b/src/exercises/22/Screenshot_6.png new file mode 100644 index 00000000..ea5a9aaa Binary files /dev/null and b/src/exercises/22/Screenshot_6.png differ diff --git a/src/exercises/22/exercise.js b/src/exercises/22/exercise.js index fbcfc74d..ab33dd18 100644 --- a/src/exercises/22/exercise.js +++ b/src/exercises/22/exercise.js @@ -5,9 +5,23 @@ function randomCalculator() { let x = 10, y = 0; + debugger; x = x * 10 + 1 - (2 / 2) * 3; y = x - 1 + 3 * 10 - 2; x = x * 2 - (1 / 3) * 3; x = x + 10 - 2 - 3 * 2; console.log(x); } + +/** + * Initially add x and y variables to Watch section in Chrome debugging console. + * and in code add a debugger statement below first line so that we can track x and y values from intial position + * if added at the top they will not be created and it shows '< not available >' + * now reload the page it will automatically stop the execution at debugger statement in line8. + * and then click on `ctrl + '` to go step by step and track x and y values after each statement. + * also observe that after executing console.log we get the last x value logged in console. + */ + + + +randomCalculator() \ No newline at end of file diff --git a/src/exercises/22/index.html b/src/exercises/22/index.html new file mode 100644 index 00000000..64fb10e2 --- /dev/null +++ b/src/exercises/22/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/src/exercises/23/exercise.js b/src/exercises/23/exercise.js index 92160dd9..1b9dd1aa 100644 --- a/src/exercises/23/exercise.js +++ b/src/exercises/23/exercise.js @@ -4,12 +4,18 @@ // Comment your understanding // Provide a fix. +// noDelayPrint() increments the page-index and send it to print. +// no, this is not referring to an instance of Printer. instead it refers to TimeOut +// since index is not present in Timeout it prints undefined +// + class Printer { constructor() { this.index = 0; } - print() { + print = () => { + // console.log(this) console.log(`Printing sheet number ${this.index}`); } @@ -18,10 +24,21 @@ class Printer { setTimeout(this.print, 0); // 0 milli-sec delay this.index++; } - + noDelayPrint() { console.log(`You asked me to print the sheet ${this.index}`); - setTimeout(this.print, 1000); //1 sec dely + setTimeout(() => { + this.print(); + }, 1000); //1 sec dely this.index++; } } + +let printer = new Printer() + + +printer.noDelayPrint(); +printer.noDelayPrint(); +printer.noDelayPrint(); +printer.noDelayPrint(); +printer.noDelayPrint(); \ No newline at end of file diff --git a/src/exercises/24/exercise.js b/src/exercises/24/exercise.js index 58d4ace3..f565225f 100644 --- a/src/exercises/24/exercise.js +++ b/src/exercises/24/exercise.js @@ -2,3 +2,7 @@ // - Share the resource link here. // - Update all the exercises you have finished above by following the style guide. // From the next exercise write your code following the style guide + +/** + * https://www.w3schools.com/js/js_conventions.asp + */ \ No newline at end of file diff --git a/src/exercises/25/exercise.js b/src/exercises/25/exercise.js index 294b8b6b..b68046cd 100644 --- a/src/exercises/25/exercise.js +++ b/src/exercises/25/exercise.js @@ -1 +1,13 @@ // Read what is a closure and give an example here + +const Incrementor = function(incBy) { + return function(val) { + return incBy + val; + } +} + +const twoInc = Incrementor(2); +const fourInc = Incrementor(4); + +console.log(twoInc(5)); +console.log(fourInc(5)) \ No newline at end of file diff --git a/src/exercises/26/exercise.js b/src/exercises/26/exercise.js index b886e948..bcd5a7d0 100644 --- a/src/exercises/26/exercise.js +++ b/src/exercises/26/exercise.js @@ -7,4 +7,32 @@ const counter = (function () { //Your code goes here. + let val = 0; + return function() { + return { + increment() { + val += 1 + }, + decrement() { + val -= 1 + }, + value() { + return val; + } + } + } })(); + + +const c = counter(); + +console.log(c.value()); + +c.increment(); +c.increment(); + +console.log(c.value()); + +c.decrement(); + +console.log(c.value()); \ No newline at end of file diff --git a/src/exercises/27/exercise.js b/src/exercises/27/exercise.js index 41ddb7f7..5059553a 100644 --- a/src/exercises/27/exercise.js +++ b/src/exercises/27/exercise.js @@ -1,5 +1,9 @@ // What is a callback? +// Callback is a function that is passed as an argument to other function. +// It can be used to call a function after a certain operation/event is done. + + function welcome(name) { setTimeout(() => { console.log(`Welcome ${name}`); @@ -7,10 +11,12 @@ function welcome(name) { } function success() { - console.log("Greetings successful!!"); + setTimeout(() => { + console.log("Greetings successful!!"); + }, 1001); } -welcome(); +welcome('User'); success(); // In the above example: diff --git a/src/exercises/28/exercise.js b/src/exercises/28/exercise.js index 37050f14..29ed3d19 100644 --- a/src/exercises/28/exercise.js +++ b/src/exercises/28/exercise.js @@ -3,7 +3,16 @@ // - Reason here why is it so. // - Explain in your own terms what are event loops? -function learnEventLoops() { + + +/** + * Event loop is like main thread in node.js + * when API's like setTimeout is invoked it pushes it into a taskQueue, which executes when the time runs out. + * So, here the console with 50% and 75% message are getting pushed into a queue and mean while learning completed is being printed in the console + * so if we are delaying the 75% message by 1millisec, using setTimeout on learning completed message with atleast 2millisec should fix the issue + */ + + function learnEventLoops() { console.log("Learning event loops"); setTimeout(function print() { console.log("50% Learning done!"); @@ -11,6 +20,12 @@ function learnEventLoops() { setTimeout(function print() { console.log("75% Learning done!"); }, 1); // 1 millisec - console.log("Learning completed!!"); + + // console.log("Learning completed!!"); + setTimeout(function print() { + console.log("Learning completed!!"); + }, 2); + } -main(); + +learnEventLoops(); diff --git a/src/exercises/29/exercise.js b/src/exercises/29/exercise.js index e841af66..35f18452 100644 --- a/src/exercises/29/exercise.js +++ b/src/exercises/29/exercise.js @@ -4,3 +4,23 @@ function calculateVolume(length, breadth, height) { return length * breadth * height; } + + +/** + * currying is a technique used to convert single function with multiple parameters into several functions of single parameter + * the function is wrapped inside an another function and so on... + * like this, the first function is passed with a parameter which returns an another function to which second parameter is passed and so on... + * then, the final answer is returned from the inner function to outer and so on... and atlast the function that we called returns the final answer. + */ + +console.log(`volume using regular function = ${calculateVolume(2, 3, 5)}`) + +function calculateVolumeByCurrying(length) { + return function(breadth) { + return function(height) { + return length * breadth * height; + } + } +} + +console.log(`volume using curry function = ${calculateVolumeByCurrying(2)(3)(5)}`) \ No newline at end of file diff --git a/src/exercises/3/exercise.js b/src/exercises/3/exercise.js index ddfc8767..48c98d1f 100644 --- a/src/exercises/3/exercise.js +++ b/src/exercises/3/exercise.js @@ -5,6 +5,10 @@ function checkIfArray(input) { let isInputAnArray = false; //... + // we can use instanceof operator to type checking + if(input instanceof Array) { + isInputAnArray = true; + } if (isInputAnArray) { console.log("Given input is an Array"); @@ -12,3 +16,5 @@ function checkIfArray(input) { console.log("Given input is not an Array"); } } + +checkIfArray([1, 2, 3, 4]) \ No newline at end of file diff --git a/src/exercises/30/exercise.js b/src/exercises/30/exercise.js index 9cf6aa66..11f31464 100644 --- a/src/exercises/30/exercise.js +++ b/src/exercises/30/exercise.js @@ -3,3 +3,27 @@ // - Stop the timer at exactly 1min // - Display it on a HTML page // - What is the function that you have used? + + +function countDown(timer) { + if(timer > 60) return; + let display = document.getElementsByClassName('timer')[0]; + if(timer == 0) { + display.innerHTML = `starting Timer...`; + } + else if(timer < 60) { + display.innerHTML = `${timer}s`; + } + else { + display.innerHTML = `1min completed.` + } + showTimer(timer + 1); +} + +function showTimer(time) { + setTimeout(() => { + countDown(time) + }, 1000); +} + +showTimer(0) \ No newline at end of file diff --git a/src/exercises/30/index.html b/src/exercises/30/index.html new file mode 100644 index 00000000..3af97309 --- /dev/null +++ b/src/exercises/30/index.html @@ -0,0 +1,18 @@ + + + + + + + Document + + +
+
+

Timer

+

+
+
+ + + \ No newline at end of file diff --git a/src/exercises/31/app.css b/src/exercises/31/app.css new file mode 100644 index 00000000..fe91c59e --- /dev/null +++ b/src/exercises/31/app.css @@ -0,0 +1,52 @@ +.container { + width: 100%; + text-align: center; + display: flex; + justify-content: center; + align-items: center; +} + +.content { + width: 100%; + display: grid; + justify-content: center; + border-bottom: 2px solid gray; + padding-bottom: 18px; +} + +.title { + font-size: 44px; +} + +#input-text { + display: block; + width: 75vw; + height: 52px; + margin-bottom: 8px; +} + +#input-text:focus { + outline: none; +} + +#lang { + font-size: 14px; + text-transform: uppercase; +} + +#btn { + background-color: blue; + color: white; + font-weight: bold; + font-size: 16px; + letter-spacing: 1px; + padding: 6px 10px; + border-radius: 5px; + border: 0; + cursor: pointer; +} + +#text-translate { + font-size: 28px; + +} \ No newline at end of file diff --git a/src/exercises/31/exercise.js b/src/exercises/31/exercise.js index 8409e2ba..c01babc1 100644 --- a/src/exercises/31/exercise.js +++ b/src/exercises/31/exercise.js @@ -11,3 +11,58 @@ // Declaration of translate // function translate(sourceString:string, targetLanguage:string) + +function getLanguages() { + let select = document.getElementById('lang') + fetch('https://google-translate1.p.rapidapi.com/language/translate/v2/languages', { + method: 'GET', + headers: { + 'Accept-Encoding': 'application/gzip', + 'X-RapidAPI-Key': 'd1dd18b801mshac813251f1cb31cp1950bdjsnffac2a264649', + 'X-RapidAPI-Host': 'google-translate1.p.rapidapi.com' + } + }) + .then(async (res) => { + let data = await res.json() + data.data.languages.forEach(element => { + var opt = document.createElement('option'); + opt.value = element.language; + opt.innerHTML = element.language; + select.appendChild(opt); + }); + }) +} + +getLanguages(); + +class Translator { + async translate(sourceString, targetLanguage) { + const encodedParams = new URLSearchParams(); + encodedParams.append("q", sourceString); + encodedParams.append("target", targetLanguage); + + const options = { + method: 'POST', + headers: { + 'content-type': 'application/x-www-form-urlencoded', + 'Accept-Encoding': 'application/gzip', + 'X-RapidAPI-Key': 'd1dd18b801mshac813251f1cb31cp1950bdjsnffac2a264649', + 'X-RapidAPI-Host': 'google-translate1.p.rapidapi.com' + }, + body: encodedParams + }; + + let res = await fetch('https://google-translate1.p.rapidapi.com/language/translate/v2', options); + res = await res.json() + let text = await res.data.translations[0].translatedText + return text + } +} + + +document.getElementById("btn").addEventListener('click', async() => { + text = document.getElementById('input-text').value; + lang = document.getElementById('lang').value; + const translator = new Translator() + document.getElementById('text-translate').innerHTML = await translator.translate(text, lang) +}) diff --git a/src/exercises/31/index.html b/src/exercises/31/index.html new file mode 100644 index 00000000..3158b2f1 --- /dev/null +++ b/src/exercises/31/index.html @@ -0,0 +1,36 @@ + + + + + + + + Document + + +
+
+ +
+
+

Translator

+
+
+ +
+

language:

+ +
+
+
+ +
+
+
+

+
+
+
+ + + \ No newline at end of file diff --git a/src/exercises/32/exercise.js b/src/exercises/32/exercise.js index f990b0a5..32f295d1 100644 --- a/src/exercises/32/exercise.js +++ b/src/exercises/32/exercise.js @@ -35,3 +35,53 @@ */ + + +class Airport { + async getInfoFromIATA(code) { + + const options = { + method: 'GET', + headers: { + 'X-RapidAPI-Key': 'd1dd18b801mshac813251f1cb31cp1950bdjsnffac2a264649', + 'X-RapidAPI-Host': 'airport-info.p.rapidapi.com' + }, + }; + + let res = await fetch(`https://airport-info.p.rapidapi.com/airport?iata=${code}`, options) + return await res.json() + } + + + async getInfoFromICAO(code) { + + const options = { + method: 'GET', + headers: { + 'X-RapidAPI-Key': 'd1dd18b801mshac813251f1cb31cp1950bdjsnffac2a264649', + 'X-RapidAPI-Host': 'airport-info.p.rapidapi.com' + }, + }; + + let res = await fetch(`https://airport-info.p.rapidapi.com/airport?icao=${code}`, options) + return await res.json() + } +} + + +document.getElementById('btn').addEventListener('click', async () => { + let airport = new Airport() + let iata = document.getElementById('iata').value + let icao = document.getElementById('icao').value + if(iata === '' && icao === '') { + alert("Enter IATA/ICAO code to get airport data.") + return; + } + if(iata) { + console.log(await airport.getInfoFromIATA(iata)) + } + if(icao) { + console.log(await airport.getInfoFromICAO(icao)) + } + +}) \ No newline at end of file diff --git a/src/exercises/32/index.html b/src/exercises/32/index.html new file mode 100644 index 00000000..adff2ae3 --- /dev/null +++ b/src/exercises/32/index.html @@ -0,0 +1,31 @@ + + + + + + + + Document + + +
+
+

Airport Details

+
+
+

IATA

+ +
+
+

ICAO

+ +
+
+
+ Get Details +
+
+
+ + + \ No newline at end of file diff --git a/src/exercises/33/exercise.js b/src/exercises/33/exercise.js index 1d66757b..2cb42da5 100644 --- a/src/exercises/33/exercise.js +++ b/src/exercises/33/exercise.js @@ -16,3 +16,14 @@ // id : 1122, // age : 29 // } + +const obj = { + name: 'Ravi', + id: 1122, + age: 29 +} +let {name, ...otherProperties} = obj + +console.log(name) +console.log(otherProperties) + diff --git a/src/exercises/34/exercise.js b/src/exercises/34/exercise.js index c018d0e3..f2b9f524 100644 --- a/src/exercises/34/exercise.js +++ b/src/exercises/34/exercise.js @@ -8,8 +8,21 @@ function multiply(num1, num2) { // multiply(2,3) - returns 6 // multiple(4) - return NaN +console.log(`multiply(2, 3) = ${multiply(2, 3)}`) +console.log(`multiply(4) = ${multiply(4)}\n\n`) + + + // Update the above function to consider a missing numbers default value as 1 +function modifiedMultiply(num1 = 1, num2 = 1) { + return num1 * num2; +} + // After the change // multiply(4) - Should return 4 // multiply() - Should return 1 + +console.log(`modifiedMultiply(2, 3) = ${modifiedMultiply(2, 3)}`) +console.log(`modifiedMultiply(4) = ${modifiedMultiply(4)}`) +console.log(`modifiedMultiply() = ${modifiedMultiply()}`) \ No newline at end of file diff --git a/src/exercises/35/exercise.js b/src/exercises/35/exercise.js index 0b86ce11..0288c157 100644 --- a/src/exercises/35/exercise.js +++ b/src/exercises/35/exercise.js @@ -10,10 +10,18 @@ const employeeRavi = { }, }; +// function getExperienceOfRavi() { +// if (employeeRavi.getExperience) { +// console.log(employeeRavi.getExperience()); +// } +// } + +// - Modify the above function to use optional chaining. + function getExperienceOfRavi() { - if (employeeRavi.getExperience) { - console.log(employeeRavi.getExperience()); + if (employeeRavi?.getExperience) { + console.log(employeeRavi?.getExperience()); } } -// - Modify the above function to use optional chaining. +console.log(getExperienceOfRavi()) \ No newline at end of file diff --git a/src/exercises/36/exercise.js b/src/exercises/36/exercise.js index 23047b1f..f9e40469 100644 --- a/src/exercises/36/exercise.js +++ b/src/exercises/36/exercise.js @@ -21,3 +21,16 @@ console.log(A() ?? C() ?? D()); console.log(B() ?? D()); //Comment the outputs below and reason + + +/** + * + * ?? operator return RHS only if the LHS has null or undefined + * + * Line 20 + * since A() and C() returned undefined and null respectively. So the returned value from D() has been printed + * + * Line 21 + * since B() doesn't have a null or undefined return value it simple prints the returned value from B() i.e, false + * + */ \ No newline at end of file diff --git a/src/exercises/4/exercise.js b/src/exercises/4/exercise.js index d977ce66..34679922 100644 --- a/src/exercises/4/exercise.js +++ b/src/exercises/4/exercise.js @@ -4,6 +4,8 @@ function greeting(firstName, lastName) { let welcomeGreeting; - + welcomeGreeting = ["Welcome", firstName, lastName].join(' '); console.log(welcomeGreeting); } + +greeting('raziq' , 'ali') \ No newline at end of file diff --git a/src/exercises/5/exercise.js b/src/exercises/5/exercise.js index 14ea345c..394fc104 100644 --- a/src/exercises/5/exercise.js +++ b/src/exercises/5/exercise.js @@ -2,9 +2,13 @@ // - Find the easiest way to do it. // - Comment the function that you used. + function binaryToDecimal(binaryString) { - let decimalValue; + // we can pass the base value of the string and actual string to `parseInt` and it will convert the string to decimal. + let decimalValue = parseInt(binaryString, 2); console.log( `Decimal for the given binary string ${binaryString} is ${decimalValue}` ); } + +binaryToDecimal("1001") \ No newline at end of file diff --git a/src/exercises/6/exercise.js b/src/exercises/6/exercise.js index a7149958..0d1313dd 100644 --- a/src/exercises/6/exercise.js +++ b/src/exercises/6/exercise.js @@ -3,5 +3,14 @@ function findTheLength(inputArray) { let lengthOfArray; - console.log(`Length of the given input array is ${lengthOfArray}`); + + if(inputArray instanceof Array) { + lengthOfArray = inputArray.length; + console.log(`Length of the given input array is ${lengthOfArray}`); + } + else { + console.log(`Invalid Array`) + } } + +findTheLength([1234, 5678, 90]); \ No newline at end of file diff --git a/src/exercises/7/exercise.js b/src/exercises/7/exercise.js index 8c18d178..497a26a1 100644 --- a/src/exercises/7/exercise.js +++ b/src/exercises/7/exercise.js @@ -4,4 +4,7 @@ function commaSeparatedString(inputArray) { // Given an Array like ["firstName","lastName"] // Return a comma separated String like "firstName,lastName" + return inputArray.join(','); } + +console.log(commaSeparatedString(['firstName', 'lastName'])); \ No newline at end of file diff --git a/src/exercises/8/exercise.js b/src/exercises/8/exercise.js index e127b842..b4b542b4 100644 --- a/src/exercises/8/exercise.js +++ b/src/exercises/8/exercise.js @@ -2,4 +2,10 @@ // - Print the given Array. One element in one line // - Use loops -function printArray(inputArray) {} +function printArray(inputArray) { + for(let i = 0; i < inputArray.length; i++) { + console.log(inputArray[i]); + } +} + +printArray([1, 2, 3, 4, 5]) \ No newline at end of file diff --git a/src/exercises/9/exercise.js b/src/exercises/9/exercise.js index d63979cf..fcfe5539 100644 --- a/src/exercises/9/exercise.js +++ b/src/exercises/9/exercise.js @@ -3,5 +3,8 @@ // - Do not use loops function multiplyArrayByTwo(inputArray) { + inputArray = inputArray.map(val => val * 2); console.log(`Given input array is ${inputArray}`); } + +multiplyArrayByTwo([1, 2, 3, 4]) \ No newline at end of file