diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..6b665aaa --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} diff --git a/src/exercises/1/exercise.js b/src/exercises/1/exercise.js index fcf3805e..766487e8 100644 --- a/src/exercises/1/exercise.js +++ b/src/exercises/1/exercise.js @@ -2,8 +2,12 @@ // - comment the reason for failure. // - Fix the issue. +/* failure due to using of const keyword +const keyword declare variable as a block constant +reassigning of varible is not possible +*/ function printOneToTen() { - for (const number = 1; number <= 10; number++) { + for (let number = 1; number <= 10; number++) { console.log(`\n${number}`); } } diff --git a/src/exercises/10/exercise.js b/src/exercises/10/exercise.js index 0deb7689..c91b7ad5 100644 --- a/src/exercises/10/exercise.js +++ b/src/exercises/10/exercise.js @@ -15,4 +15,11 @@ } ] */ -function findVoteEligibleCandidates(populationList) {} +function findVoteEligibleCandidates(populationList) { + let elibileCandidates = populationList.filter(function (candidates) { + if (candidates.age > 18) { + return true; + } + }); + console.log(elibileCandidates); +} diff --git a/src/exercises/11/exercise.js b/src/exercises/11/exercise.js index 0317df9e..bddc1e66 100644 --- a/src/exercises/11/exercise.js +++ b/src/exercises/11/exercise.js @@ -2,4 +2,9 @@ // - Use Array methods // - Do not use loops -function sumOfArray(inputArray) {} +function sumOfArray(inputArray) { + let result = inputArray.reduce(function (total, age) { + return total + age; + }, 0); + console.log(result); +} diff --git a/src/exercises/12/exercise.js b/src/exercises/12/exercise.js index dd4df555..644f5746 100644 --- a/src/exercises/12/exercise.js +++ b/src/exercises/12/exercise.js @@ -5,5 +5,12 @@ function errorFunction() { } function errorCaller() { - errorFunction(); + //try block is to run the code to overcome the error exceptions + //The catch statement allows you to define a block of code to be executed, if an error occurs in the try block. + //both try block and catch block declare as a pair + try { + errorFunction(); + } catch (err) { + console.log(err); + } } diff --git a/src/exercises/13/exercise.js b/src/exercises/13/exercise.js index cb1ceaeb..aca4d2b6 100644 --- a/src/exercises/13/exercise.js +++ b/src/exercises/13/exercise.js @@ -5,6 +5,13 @@ 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"; + } + return "NotEligible"; + }, }; diff --git a/src/exercises/14/exercise.js b/src/exercises/14/exercise.js index 6e47dd07..946731c8 100644 --- a/src/exercises/14/exercise.js +++ b/src/exercises/14/exercise.js @@ -8,6 +8,7 @@ const employee = { id: 1122, getFullName: function () { //Copy the solution from the exercise before this. + return this.firstName + " " + this.lastName; }, }; @@ -15,3 +16,7 @@ const newEmployee = { firstName: "New", lastName: "Employee", }; +//call function used to invoke (call) a method with an owner object as an argument. +//by using call(), an object can use a method belonging to another object. + +employee.getFullName.call(newEmployee); diff --git a/src/exercises/15/exercise.js b/src/exercises/15/exercise.js index 1c4a7d48..0e864cf0 100644 --- a/src/exercises/15/exercise.js +++ b/src/exercises/15/exercise.js @@ -1,13 +1,17 @@ // Convert the below functions into arrow functions. - +/* const greetings = function () { return "Hello World"; }; +*/ +const greetings = () => "Hello World"; +/* const isEven = function (num) { if (num % 2 == 0) { return true; } else { return false; } -}; +};*/ +const isEven = (num) => (num % 2 == 0 ? true : false); diff --git a/src/exercises/16/exercise.js b/src/exercises/16/exercise.js index 01561081..8b1ef7b9 100644 --- a/src/exercises/16/exercise.js +++ b/src/exercises/16/exercise.js @@ -2,6 +2,20 @@ // - Identify the class variables // - Create function for all the possible functionalities. -class Leave { +export default class Leave { //... + constructor(firstName, lastName, id, gender) { + this.firstName = firstName; + this.lastName = lastName; + this.id = id; + this.gender = gender; + } + display() { + return ( + this.firstName + + " " + + this.lastName + ); + } } + diff --git a/src/exercises/17/exercise.js b/src/exercises/17/exercise.js index f344d030..9247f670 100644 --- a/src/exercises/17/exercise.js +++ b/src/exercises/17/exercise.js @@ -1 +1,21 @@ // Extend the Leave class, to create different classes for different Leave types. + +import Leave from "../16/exercise.js"; +export class SickLeave extends Leave { + constructor(firstName, lastName, id, gender) { + super(firstName, lastName, id, gender); + this.reason = "Sick"; + } + show() { + console.log(this.display() + " applying " + this.reason + " Leave"); + } +} +export class CasualLeave extends Leave { + constructor(firstName, lastName, id, gender) { + super(firstName, lastName, id, gender); + this.reason = "Casual"; + } + show() { + console.log(this.display() + " applying " + this.reason + " Leave"); + } +} diff --git a/src/exercises/18/exercise.js b/src/exercises/18/exercise.js index e0fd230c..19bd5258 100644 --- a/src/exercises/18/exercise.js +++ b/src/exercises/18/exercise.js @@ -1,3 +1,31 @@ // 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 { SickLeave, CasualLeave } from "../17/exercise"; +const ob1 = new SickLeave("Sai", "Ram", 21, "m"); +ob1.show(); + +/* +about import : +The import keyword allows you to import a JavaScript module. Importing a module allows you +to pull any items from that module into the current code file. When we import a module, we + start the expression with the import keyword. Then, we identify what parts of the module we are +going to import. Then, we follow that with the "from" keyword, and finally we finish with the path +to the module file. The from keyword and file path tell the interpreter where to find the module we are importing. +Syntax: + import { export_function,varibles } from "file_path" + //when we using name exports + import export_function,varible from "./file_path"; + //when we using defaule exports + +about export: +The export keyword make the selected items in the module avaliable to other other module. +there different types of export types: + //name export + export { ItemName1,ItemName2.. }; + export let name1, name2..; + //deafult export + export default name1; + export default class Class_name; +*/ \ No newline at end of file diff --git a/src/exercises/19/exercise.js b/src/exercises/19/exercise.js index ec707066..8fda280d 100644 --- a/src/exercises/19/exercise.js +++ b/src/exercises/19/exercise.js @@ -1,3 +1,13 @@ // Comment below what did you understand from the previous 3 exercises about // - prototype // - __proto__ + +/* +Prototypes are the mechanism by which JavaScript objects inherit features from one another. +Prototypes are a powerful and very flexible feature of JavaScript, making it possible to reuse +code and combine objects. +Every object in JavaScript has a built-in property, which is called its prototype. The prototype +is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. +The chain ends when we reach a prototype that has null for its own prototype. +__proto__ is one of the function of prototype +*/ \ No newline at end of file diff --git a/src/exercises/2/exercise.js b/src/exercises/2/exercise.js index a9a49c16..14abade5 100644 --- a/src/exercises/2/exercise.js +++ b/src/exercises/2/exercise.js @@ -2,12 +2,12 @@ // - comment the reason for failure. // - Fix the issue. +//let is a block variable, there can not accessed from out of the block { } function divideTenByNumber(number) { if (number != 0) { - let result = 10 / number; + var result = 10 / number; } else { - let result = "Indeterminate"; + var result = "Indeterminate"; } - console.log(`Result after dividing 10 by ${number} is ${result}`); } diff --git a/src/exercises/20/exercise.js b/src/exercises/20/exercise.js index 9a30e605..067bb95c 100644 --- a/src/exercises/20/exercise.js +++ b/src/exercises/20/exercise.js @@ -1,3 +1,16 @@ // What is debugger; // - Comment your understanding of it. // - Paste the link of the MDN article here. + +/* +debugging means the process of finding the bugs or error in the code. debugger will help to understand +the code line by line,by using this identification of dugs in code is possible. +we can start debugging the code by placing the keyword - debugger +The debugger statement invokes any available debugging functionality, such as setting a breakpoint. +If no debugging functionality is available, this statement has no effect + +By using this debugger concept we can understand the line by line execution of the code, and find the place of +error occuring. + +MDN article Link: (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) +*/ diff --git a/src/exercises/21/exercise.js b/src/exercises/21/exercise.js index 4fe9d13e..ed69bf66 100644 --- a/src/exercises/21/exercise.js +++ b/src/exercises/21/exercise.js @@ -1 +1,18 @@ // What are the different levels of logs and how do we print them to the console? + +/*JavaScript has support for logging messages at various levels. They are as follows, with their associated method and a description of their output in the console: + +Plaintext—console.log() outputs unstyled text. +Info—console.info() typically outputs text with a blue background. +Warn—console.warn() typically outputs text with a yellow background. +Error—console.error() typically outputs text with a red background. +*/ + +console.log("Hello World"); +console.dir("Hello World"); //outputs unstyled text. + +console.error("error"); //typically outputs text with a red background. + +console.warn("warning"); //typically outputs text with a yellow background. + +console.info("information"); //display the information diff --git a/src/exercises/22/exercise.js b/src/exercises/22/exercise.js index fbcfc74d..0e41b421 100644 --- a/src/exercises/22/exercise.js +++ b/src/exercises/22/exercise.js @@ -3,11 +3,12 @@ // - What is "Watch" in chrome debugging console. Share a screenshot of the watch variables at each step. function randomCalculator() { + debugger; let x = 10, y = 0; 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); + console.log(x); //197 } diff --git a/src/exercises/22/screenshots/Screenshot (1).png b/src/exercises/22/screenshots/Screenshot (1).png new file mode 100644 index 00000000..6d815be7 Binary files /dev/null and b/src/exercises/22/screenshots/Screenshot (1).png differ diff --git a/src/exercises/22/screenshots/Screenshot (2).png b/src/exercises/22/screenshots/Screenshot (2).png new file mode 100644 index 00000000..5a1062aa Binary files /dev/null and b/src/exercises/22/screenshots/Screenshot (2).png differ diff --git a/src/exercises/22/screenshots/Screenshot (3).png b/src/exercises/22/screenshots/Screenshot (3).png new file mode 100644 index 00000000..6de3cfdf Binary files /dev/null and b/src/exercises/22/screenshots/Screenshot (3).png differ diff --git a/src/exercises/22/screenshots/Screenshot (4).png b/src/exercises/22/screenshots/Screenshot (4).png new file mode 100644 index 00000000..a260fd4d Binary files /dev/null and b/src/exercises/22/screenshots/Screenshot (4).png differ diff --git a/src/exercises/22/screenshots/Screenshot (5).png b/src/exercises/22/screenshots/Screenshot (5).png new file mode 100644 index 00000000..93c38cb3 Binary files /dev/null and b/src/exercises/22/screenshots/Screenshot (5).png differ diff --git a/src/exercises/22/screenshots/Screenshot (6).png b/src/exercises/22/screenshots/Screenshot (6).png new file mode 100644 index 00000000..702f7d29 Binary files /dev/null and b/src/exercises/22/screenshots/Screenshot (6).png differ diff --git a/src/exercises/23/exercise.js b/src/exercises/23/exercise.js index 92160dd9..3e99c9c8 100644 --- a/src/exercises/23/exercise.js +++ b/src/exercises/23/exercise.js @@ -25,3 +25,14 @@ class Printer { this.index++; } } + +/* +Here there are two noDelayPrint() and the last function will overcome the above functions and +start execution. + +yes, 'this' inside print function referrring to an instance of Printer. +In JavaScript, the this keyword refers to an object. + +In this class there are two methods with same name, i have observed that the last method is +overridding the above method, and start execution. +*/ diff --git a/src/exercises/24/exercise.js b/src/exercises/24/exercise.js index 58d4ace3..cca47d9b 100644 --- a/src/exercises/24/exercise.js +++ b/src/exercises/24/exercise.js @@ -2,3 +2,6 @@ // - 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 + + +//Resource Link:(https://www.w3schools.com/js/js_conventions.asp) diff --git a/src/exercises/25/exercise.js b/src/exercises/25/exercise.js index 294b8b6b..a321e80c 100644 --- a/src/exercises/25/exercise.js +++ b/src/exercises/25/exercise.js @@ -1 +1,19 @@ // Read what is a closure and give an example here + + +/*A closure is the combination of a function bundled together (enclosed) with references to its +surrounding state.In other words, a closure gives you access to an outer +function's scope from an inner function. In JavaScript, closures are created every time a function +is created, at function creation time. +inner function can access the outer function scope and variables by closure in javaScript +*/ + +function init() { + var name = "Mozilla"; // name is a local variable created by init + function displayName() { + // displayName() is the inner function, a closure + console.log(name); // use variable declared in the parent function + } + displayName(); +} +init(); \ No newline at end of file diff --git a/src/exercises/26/exercise.js b/src/exercises/26/exercise.js index b886e948..e395d301 100644 --- a/src/exercises/26/exercise.js +++ b/src/exercises/26/exercise.js @@ -7,4 +7,56 @@ const counter = (function () { //Your code goes here. + let privateCounter = 0; + function changeBy(val) { + privateCounter += val; + } + + return { + increment() { + changeBy(1); + }, + + decrement() { + changeBy(-1); + }, + + value() { + return privateCounter; + }, + }; })(); + +console.log(counter.value()); // 0 + +console.log(counter.increment()); +console.log(counter.increment()); + +console.log(counter.value()); //2 + +console.log(counter.decrement()); + +console.log(counter.value()); //1 + + +//yes, in this exercise we use anonymus function +//At line number 8 +/* (function(){ + /.. +})(); +*/ + +//IIFE(immediately invoking function expression) +/*Syntax: +(function(){ + //statements.. +})(); + +(()=> + //statements.. +)(); + +In Javascript the function are invoked immediately with out call them with the help of IIFE +*/ + + diff --git a/src/exercises/27/exercise.js b/src/exercises/27/exercise.js index 41ddb7f7..5e7addb7 100644 --- a/src/exercises/27/exercise.js +++ b/src/exercises/27/exercise.js @@ -1,8 +1,16 @@ // What is a callback? -function welcome(name) { +/*A callback function is a function passed into another function as an argument. +This technique allows a function to call another function +A callback function can run after another function has finished +A callback ia a function that is to be executed after another function has +finished executing--hence the name "callback" +*/ + +function welcome(name, myCallBack) { setTimeout(() => { console.log(`Welcome ${name}`); + myCallBack() }, 1000); } @@ -10,8 +18,7 @@ function success() { console.log("Greetings successful!!"); } -welcome(); -success(); +welcome("Apxor", success); // In the above example: // - Before welcoming, success message is printed. diff --git a/src/exercises/28/exercise.js b/src/exercises/28/exercise.js index 37050f14..4a7a395e 100644 --- a/src/exercises/28/exercise.js +++ b/src/exercises/28/exercise.js @@ -13,4 +13,16 @@ function learnEventLoops() { }, 1); // 1 millisec console.log("Learning completed!!"); } -main(); +/* +output: +Learning event lopps +Learning completed!! +50% Learning done! +75% Learning done! + +JavaScript execute the code by line by line, javaScript follow synchronous. +while in the above code we use setTimeout function which make the execution asynchronous +those setTimeout function will executed after the time which was initialized. +javascript doesn't wait utill the execution of asynchronous functions.It will execute remaining +lines of code. +*/ diff --git a/src/exercises/29/exercise.js b/src/exercises/29/exercise.js index e841af66..b27cbc5e 100644 --- a/src/exercises/29/exercise.js +++ b/src/exercises/29/exercise.js @@ -1,6 +1,16 @@ // - Explain currying. // - Convert the below function to curry function -function calculateVolume(length, breadth, height) { - return length * breadth * height; +/* +Currying in javascript means transformation of the function of multiple +arguments into several functions of a single argument in sequence. +*/ + +function calculateVolume(length) { + return function (breadth) { + return function (height) { + return length * breadth * height; + }; + }; } +console.log(calculateVolume(2)(3)(4)); diff --git a/src/exercises/3/exercise.js b/src/exercises/3/exercise.js index ddfc8767..37e08e2c 100644 --- a/src/exercises/3/exercise.js +++ b/src/exercises/3/exercise.js @@ -5,7 +5,10 @@ function checkIfArray(input) { let isInputAnArray = false; //... + isInputAnArray = Array.isArray(input); + //by using isArray function we can check the input is array or not + //It will return true if the input is array otherwise return false if (isInputAnArray) { console.log("Given input is an Array"); } else { diff --git a/src/exercises/30/exercise.js b/src/exercises/30/exercise.js index 9cf6aa66..68339d35 100644 --- a/src/exercises/30/exercise.js +++ b/src/exercises/30/exercise.js @@ -3,3 +3,13 @@ // - Stop the timer at exactly 1min // - Display it on a HTML page // - What is the function that you have used? + +var timmer = 0; + +var x = setInterval(function () { + timmer += 1; + if (timmer === 60) { + clearInterval(x); + } + document.getElementById("demo").innerHTML = timmer; +}, 1000); diff --git a/src/exercises/30/index.html b/src/exercises/30/index.html new file mode 100644 index 00000000..dc21a189 --- /dev/null +++ b/src/exercises/30/index.html @@ -0,0 +1,21 @@ + + + + + + + + +

+ + + + + diff --git a/src/exercises/31/exercise.js b/src/exercises/31/exercise.js index 8409e2ba..060ee8ec 100644 --- a/src/exercises/31/exercise.js +++ b/src/exercises/31/exercise.js @@ -11,3 +11,64 @@ // Declaration of translate // function translate(sourceString:string, targetLanguage:string) + +const options = { + method: "GET", + headers: { + "Accept-Encoding": "application/gzip", + "X-RapidAPI-Key": "00f1f0b8femsh3ac84db90091267p1abf61jsn5a72a6ac83dd", + "X-RapidAPI-Host": "google-translate1.p.rapidapi.com", + }, +}; + +fetch( + "https://google-translate1.p.rapidapi.com/language/translate/v2/languages", + options +) + .then((response) => response.json()) + .then((response) => { + var opt = ""; + response.data.languages.forEach((element) => { + opt += ``; + }); + document.querySelector("#languageTo").innerHTML = opt; + }); + +class Translator { + translate(sourceText, targetLanguage) { + const encodedParams = new URLSearchParams(); + encodedParams.append("q", sourceText); + 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": "00f1f0b8femsh3ac84db90091267p1abf61jsn5a72a6ac83dd", + "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) => { + document.querySelector("#textAreaOutput").innerHTML = + response.data.translations[0].translatedText; + }) + .catch((err) => console.error(err)); + } +} + +let dummy = document.querySelector("#enter"); +dummy.addEventListener("click", () => { + text = document.getElementById("textAreaInput").value; + lang = document.getElementById("languageTo").value; + const translator = new Translator(); + translator.translate(text, lang); +}); diff --git a/src/exercises/31/index.html b/src/exercises/31/index.html new file mode 100644 index 00000000..09c88e56 --- /dev/null +++ b/src/exercises/31/index.html @@ -0,0 +1,40 @@ + + + + + + Document + + + +
+

Translator

+
+ + +
+ +
+ + + + diff --git a/src/exercises/32/exercise.js b/src/exercises/32/exercise.js index f990b0a5..184c4fdb 100644 --- a/src/exercises/32/exercise.js +++ b/src/exercises/32/exercise.js @@ -32,6 +32,56 @@ "uct": 330, "website": "http://www.hyderabad.aero/" } +*/ +class Airport { + async getInfoFromIATA(IATAcode) { + const options = { + method: "GET", + headers: { + "X-RapidAPI-Key": "00f1f0b8femsh3ac84db90091267p1abf61jsn5a72a6ac83dd", + "X-RapidAPI-Host": "airport-info.p.rapidapi.com", + }, + }; + let response = await fetch( + `https://airport-info.p.rapidapi.com/airport?iata=${IATAcode}`, + options + ); + return await response.json(); -*/ + // .then((response) => response.json()) + // .then((response) => console.log(response)) + // .catch((err) => console.error("error")); + } + async getInfoFromICAO(ICAOCode) { + const options = { + method: "GET", + headers: { + "X-RapidAPI-Key": "00f1f0b8femsh3ac84db90091267p1abf61jsn5a72a6ac83dd", + "X-RapidAPI-Host": "airport-info.p.rapidapi.com", + }, + }; + + let response = await fetch( + `https://airport-info.p.rapidapi.com/airport?icao=${ICAOCode}`, + options + ); + return await response.json(); + } +} + +let button = document.querySelector("#enter"); +button.addEventListener("click", async () => { + let obj = new Airport(); + var Iata = document.getElementById("IATAText").value; + var Icao = document.getElementById("ICAOText").value; + if (Iata === "" && Icao === "") { + alert("enter any one text feild"); + return; + } + if (Iata) { + console.log(obj.getInfoFromIATA(Iata)); + } else { + console.log(obj.getInfoFromICAO(Icao)); + } +}); diff --git a/src/exercises/32/index.html b/src/exercises/32/index.html new file mode 100644 index 00000000..7f88ed6d --- /dev/null +++ b/src/exercises/32/index.html @@ -0,0 +1,23 @@ + + + + + + Document + + + +
+ + + +
+ +
+ + + diff --git a/src/exercises/33/exercise.js b/src/exercises/33/exercise.js index 1d66757b..ee737aaa 100644 --- a/src/exercises/33/exercise.js +++ b/src/exercises/33/exercise.js @@ -16,3 +16,12 @@ // id : 1122, // age : 29 // } +const obj = { + name: "Ravi", + id: 1122, + age: 29, +}; +const { name, ...otherProperties } = obj; + +console.log(name); //Ravi +console.log(otherProperties); //{id:1122,age:29} diff --git a/src/exercises/34/exercise.js b/src/exercises/34/exercise.js index c018d0e3..cc0b9007 100644 --- a/src/exercises/34/exercise.js +++ b/src/exercises/34/exercise.js @@ -1,10 +1,12 @@ // 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)); //6 +console.log(multiply(4)); //4 // 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..45f3dc2b 100644 --- a/src/exercises/35/exercise.js +++ b/src/exercises/35/exercise.js @@ -11,9 +11,7 @@ const employeeRavi = { }; function getExperienceOfRavi() { - if (employeeRavi.getExperience) { - console.log(employeeRavi.getExperience()); - } + console.log(employeeRavi?.getExperience()); } // - Modify the above function to use optional chaining. diff --git a/src/exercises/36/exercise.js b/src/exercises/36/exercise.js index 23047b1f..4052b568 100644 --- a/src/exercises/36/exercise.js +++ b/src/exercises/36/exercise.js @@ -17,7 +17,16 @@ function D() { console.log("D was called"); return "foo"; } -console.log(A() ?? C() ?? D()); +console.log(A() ?? C() ?? D()); // console.log(B() ?? D()); //Comment the outputs below and reason + +/*output: +A was called +c was called +d was called +foo //(undefined ?? null ?? foo) +b was called +false //(false ?? ...) +*/ diff --git a/src/exercises/4/exercise.js b/src/exercises/4/exercise.js index d977ce66..d46adc46 100644 --- a/src/exercises/4/exercise.js +++ b/src/exercises/4/exercise.js @@ -3,7 +3,7 @@ // - Comment its uses. function greeting(firstName, lastName) { - let welcomeGreeting; - + let welcomeGreeting = `${firstName} ${lastName}`; + //using template literal to replacing the variables with real values console.log(welcomeGreeting); } diff --git a/src/exercises/5/exercise.js b/src/exercises/5/exercise.js index 14ea345c..b65b9c82 100644 --- a/src/exercises/5/exercise.js +++ b/src/exercises/5/exercise.js @@ -3,7 +3,12 @@ // - Comment the function that you used. function binaryToDecimal(binaryString) { - let decimalValue; + let decimalValue = 0; + let len = binaryString.length; //find the length of binaryString + for (let i = 0; i < len; i++) { + //when character is "1" the Math.pow function will return otherwise return 0 + decimalValue += binaryString[i] == "1" ? Math.pow(2, len - i - 1) : 0; + } console.log( `Decimal for the given binary string ${binaryString} is ${decimalValue}` ); diff --git a/src/exercises/6/exercise.js b/src/exercises/6/exercise.js index a7149958..2d184f3e 100644 --- a/src/exercises/6/exercise.js +++ b/src/exercises/6/exercise.js @@ -2,6 +2,7 @@ // - Also validate the input. Accept the input only if its an array. function findTheLength(inputArray) { - let lengthOfArray; + //if its an array then it will return length of the array otherwise return invalid + let lengthOfArray = Array.isArray(inputArray) ? inputArray.length : "invalid"; console.log(`Length of the given input array is ${lengthOfArray}`); } diff --git a/src/exercises/7/exercise.js b/src/exercises/7/exercise.js index 8c18d178..6e58a7b7 100644 --- a/src/exercises/7/exercise.js +++ b/src/exercises/7/exercise.js @@ -4,4 +4,8 @@ function commaSeparatedString(inputArray) { // Given an Array like ["firstName","lastName"] // Return a comma separated String like "firstName,lastName" + + //using join function all elements in the array will join as one string + let result = inputArray.join(","); + console.log(result); } diff --git a/src/exercises/8/exercise.js b/src/exercises/8/exercise.js index e127b842..307b4148 100644 --- a/src/exercises/8/exercise.js +++ b/src/exercises/8/exercise.js @@ -2,4 +2,19 @@ // - Print the given Array. One element in one line // - Use loops -function printArray(inputArray) {} +function printArray(inputArray) { + //using for loop + for (let i = 0; i < inputArray.length; i++) { + console.log(inputArray[i]); + } + //using forin loop + //ForIn loop will take indexs of an array + for (let ele in inputArray) { + console.log(inputArray[ele]); + } + //using ForOf loop + //ForOf will take element from an Array + for (let ele of inputArray) { + console.log(ele); + } +} diff --git a/src/exercises/9/exercise.js b/src/exercises/9/exercise.js index d63979cf..80be1127 100644 --- a/src/exercises/9/exercise.js +++ b/src/exercises/9/exercise.js @@ -3,5 +3,7 @@ // - Do not use loops function multiplyArrayByTwo(inputArray) { + //by using map function + inputArray = inputArray.map((input) => 2 * input); console.log(`Given input array is ${inputArray}`); }