diff --git a/src/exercises/1/exercise.js b/src/exercises/1/exercise.js index fcf3805e..10236b27 100644 --- a/src/exercises/1/exercise.js +++ b/src/exercises/1/exercise.js @@ -3,7 +3,8 @@ // - Fix the issue. function printOneToTen() { - for (const number = 1; number <= 10; number++) { + for (let number = 1; number <= 10; number++) { console.log(`\n${number}`); } } +printOneToTen(); diff --git a/src/exercises/10/exercise.js b/src/exercises/10/exercise.js index 0deb7689..381bb0de 100644 --- a/src/exercises/10/exercise.js +++ b/src/exercises/10/exercise.js @@ -3,16 +3,23 @@ // - Do not use loops // Example of inputArray -/* - [ - { - name:"Ravi", - age:28 - }, - { - name:"Teja", - age:28 - } - ] -*/ -function findVoteEligibleCandidates(populationList) {} + +const voters = [ + { + name: "Ravi", + age: 28, + }, + { + name: "Teja", + age: 8, + }, +]; + +function findVoteEligibleCandidates(populationList) { + // const eligibleVoters = populationList.filter(function (person) { + // return person.age > 18; + // }); + const eligibleVoters = populationList.filter((person) => person.age > 18); + console.log(eligibleVoters); +} +findVoteEligibleCandidates(voters); diff --git a/src/exercises/11/exercise.js b/src/exercises/11/exercise.js index 0317df9e..98384e5f 100644 --- a/src/exercises/11/exercise.js +++ b/src/exercises/11/exercise.js @@ -1,5 +1,26 @@ // Complete the below function. Print the sum of the elements in the given array. // - Use Array methods // - Do not use loops +//we will use reduce method it has extra argument in the function which is called accumulator +function sumOfArray(inputArray) { + const sum = inputArray.reduce(function (acc, val, index, arr) { + return acc + val; + }, 0); + return sum; +} +//the second argument of the redduce method is the intial value of the accumulator +//it can be any number -function sumOfArray(inputArray) {} +let inputArray = [1, 2, 3, 4, 5, 56, 33]; +console.log(sumOfArray(inputArray)); +//reduce cannot be used only for sum it can be used for finding the maximum elements +//it can be used whenever result should be single value + +//chaining of the methods together +const movements = [200, 450, -400, 3000, -650, -130, 70, 1300]; + +const totalDeposit = movements + .filter((mov) => mov > 0) + .map((mov) => mov * 1.1) + .reduce((acc, mov) => acc + mov, 0); +console.log(totalDeposit); diff --git a/src/exercises/12/exercise.js b/src/exercises/12/exercise.js index dd4df555..85d95542 100644 --- a/src/exercises/12/exercise.js +++ b/src/exercises/12/exercise.js @@ -7,3 +7,9 @@ function errorFunction() { function errorCaller() { errorFunction(); } + +try { + errorCaller(); +} catch (e) { + console.log(e); +} diff --git a/src/exercises/13/exercise.js b/src/exercises/13/exercise.js index cb1ceaeb..883e193b 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 () { + console.log(this.firstName + " " + this.lastName); + }, + checkVoteEligiblity: function () { + this.age > 18 + ? console.log("Eligible to vote") + : console.log("Not eligible to vote"); + }, }; +employee.getFullName(); +employee.checkVoteEligiblity(); diff --git a/src/exercises/14/exercise.js b/src/exercises/14/exercise.js index 6e47dd07..370f99c3 100644 --- a/src/exercises/14/exercise.js +++ b/src/exercises/14/exercise.js @@ -7,6 +7,7 @@ const employee = { age: 29, id: 1122, getFullName: function () { + console.log(this.firstName + " " + this.lastName); //Copy the solution from the exercise before this. }, }; @@ -15,3 +16,6 @@ const newEmployee = { firstName: "New", lastName: "Employee", }; +newEmployee.getFullName = employee.getFullName; +newEmployee.getFullName(); +//we can copy the properties(methods and data) from one object another another object in js diff --git a/src/exercises/15/exercise.js b/src/exercises/15/exercise.js index 1c4a7d48..73f60b25 100644 --- a/src/exercises/15/exercise.js +++ b/src/exercises/15/exercise.js @@ -4,10 +4,23 @@ const greetings = function () { return "Hello World"; }; -const isEven = function (num) { - if (num % 2 == 0) { - return true; - } else { - return false; - } +// const isEven = function (num) { +// if (num % 2 == 0) { +// return true; +// } else { +// return false; +// } +// }; +//converting to arrow function + +const greet = () => { + return "Hello World"; +}; + +console.log(greet()); + +const isEven = (num) => { + let result = num % 2 == 0 ? true : false; + return result; }; +console.log(isEven(10)); diff --git a/src/exercises/16/exercise.js b/src/exercises/16/exercise.js index 01561081..c1da744a 100644 --- a/src/exercises/16/exercise.js +++ b/src/exercises/16/exercise.js @@ -2,6 +2,18 @@ // - Identify the class variables // - Create function for all the possible functionalities. -class Leave { +export class Leave { + constructor(name, numberofdays) { + this.name = name; + this.numberofdays = numberofdays; + } + generateLeaveMessage() { + console.log( + `${this.name} is on leave for ${this.numberofdays} days because ${this.reason}` + ); + } //... } +// const Bhavana = new Leave("Bhavana", 2, "not feeling well"); +// console.log(Bhavana); +// Bhavana.generateLeaveMessage(); diff --git a/src/exercises/17/exercise.js b/src/exercises/17/exercise.js index f344d030..9eb1a710 100644 --- a/src/exercises/17/exercise.js +++ b/src/exercises/17/exercise.js @@ -1 +1,46 @@ -// Extend the Leave class, to create different classes for different Leave types. +// Extend the Leave class, to create different classes for different Leave types.; +// import { Leave } from "../16/exercise.js"; + +import { Leave } from "../16/exercise.js"; +class SickLeave extends Leave { + constructor(name, age, reason = "sick Leave") { + super(name, age); + this.reason = reason; + } + generatingReason() { + super.generateLeaveMessage(); + } +} + +const sl = new SickLeave("Bhavana", 1); +sl.generatingReason(); +class Vacation extends Leave { + constructor(name, age, reason = "Vacation") { + super(name, age); + this.reason = reason; + } + generatingReason() { + super.generateLeaveMessage(); + } +} +const v = new Vacation("Bhavana", 4); +v.generatingReason(); + +//static = a method or property that belongs to a class +//not any one object +// class Car { +// static numberOfCars = 0; +// static beginRace() { +// console.log("The Race has begun"); +// } +// constructor(model) { +// this.model = model; +// Car.numberOfCars += 1; +// } +// } +// let car1 = new Car("Mustang"); +// let car2 = new Car("Corvette"); +// let car3 = new Car("Corvette"); + +// console.log(Car.numberOfCars); +// Car.beginRace(); diff --git a/src/exercises/17/index.html b/src/exercises/17/index.html new file mode 100644 index 00000000..bcaa91c8 --- /dev/null +++ b/src/exercises/17/index.html @@ -0,0 +1,9 @@ + + + + + + +

Hello

+ + \ No newline at end of file diff --git a/src/exercises/18/exercise.js b/src/exercises/18/exercise.js index e0fd230c..c393cb08 100644 --- a/src/exercises/18/exercise.js +++ b/src/exercises/18/exercise.js @@ -1,3 +1,16 @@ // Use Javascript modules to create seperate files for each of the Leave type classes. // - Comment your understanding about import // - Comment your understanding about export + +/* +there are two types of export +named export and default export +we export usinfg export keyword +export default we can send the value directly +when imported no need to use {} +*/ + +/* import modules from diffrenet pages and can be used in our code +we can import properties methods and classes */ + +//same exrcise in 16 and 17 diff --git a/src/exercises/19/exercise.js b/src/exercises/19/exercise.js index ec707066..45a0855b 100644 --- a/src/exercises/19/exercise.js +++ b/src/exercises/19/exercise.js @@ -1,3 +1,27 @@ // Comment below what did you understand from the previous 3 exercises about // - prototype // - __proto__ + +/* +protoype of a class has access to all the properties and methods in it +It can be accessed by all the instances created +If there are 100 instances for a particular class +and same code written tthere will be duplication of code +and wastage of resources will be there +*/ + +/* +__proto__ is property defined for every instance +it has all the properties and methods in it + */ + +class Car { + constructor(name, color) { + (this.name = name), (this.color = color); + } +} + +const c1 = new Car("Brezza", "white"); +console.log(Car.prototype); +console.log(c1.__proto__); +console.log(Car.prototype === c1.__proto__); diff --git a/src/exercises/2/exercise.js b/src/exercises/2/exercise.js index a9a49c16..4c93887d 100644 --- a/src/exercises/2/exercise.js +++ b/src/exercises/2/exercise.js @@ -3,11 +3,15 @@ // - Fix the issue. function divideTenByNumber(number) { + 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(3); + +//result is defined but it is not in the scope of the function it is the scope of the if else block diff --git a/src/exercises/20/exercise.js b/src/exercises/20/exercise.js index 9a30e605..d9186cfe 100644 --- a/src/exercises/20/exercise.js +++ b/src/exercises/20/exercise.js @@ -1,3 +1,17 @@ // What is debugger; // - Comment your understanding of it. // - Paste the link of the MDN article here. + +//when we write the debugger keyword in code the breakpoint +//will be set. +//If no debugging functionality then debugger will not have any effect + +//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger +function helloWorld() { + let a = 10; + let b = 20; + let c = 30; + debugger; + console.log(a + b + c); +} +helloWorld(); diff --git a/src/exercises/21/exercise.js b/src/exercises/21/exercise.js index 4fe9d13e..e9ed2818 100644 --- a/src/exercises/21/exercise.js +++ b/src/exercises/21/exercise.js @@ -1 +1,59 @@ // What are the different levels of logs and how do we print them to the console? +let x = 1; +let y = 2; +let z = 3; +console.log({ x, y, z }); +let user = { + name: "Jesse", + contact: { + email: "codestackr@gmail.com", + }, +}; +console.table(user); +// console.log(user); +// console.log({ user }); +console.log("Console log"); +console.info("Console info"); +console.debug("console debug"); +console.warn("console warn"); +console.error("console error"); + +//optional logs +//the statement will be logged if the condiiton is false +let isWorking = false; +console.assert(isWorking, "App is not working"); +for (let i = 0; i < 10; i++) { + console.count("Counter 1"); +} +console.countReset("Counter 1"); +//this is used to reset the counter +console.count("Counter 1"); + +console.time(); +setTimeout(() => { + console.timeEnd(); +}, 5000); +setTimeout(() => { + console.timeLog(); +}, 2000); + +console.groupCollapsed(); +console.log("I am in a group"); +console.log("I am also in a group"); +console.groupEnd(); +console.log( + "%c This is yellow text on a blue background.", + "color:yellow; background-color:blue" +); +//console.clear(); //clear the console + +// function one() { +// two(); +// } +// function two() { +// three(); +// } +// function three() { +// console.trace(); +// } +// one(); diff --git a/src/exercises/22/exercise.js b/src/exercises/22/exercise.js index fbcfc74d..b3753f87 100644 --- a/src/exercises/22/exercise.js +++ b/src/exercises/22/exercise.js @@ -5,9 +5,12 @@ function randomCalculator() { 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; + x = x * 10 + 1 - (2 / 2) * 3; //x=98 + y = x - 1 + 3 * 10 - 2; //y=125 + x = x * 2 - (1 / 3) * 3; //x=195 + x = x + 10 - 2 - 3 * 2; //x=197 console.log(x); } +randomCalculator(); +//watch windfdow is used to validate the particular value of the variable +//it is generrally used for checking purpose diff --git a/src/exercises/22/index.html b/src/exercises/22/index.html new file mode 100644 index 00000000..40d7291d --- /dev/null +++ b/src/exercises/22/index.html @@ -0,0 +1,8 @@ + + + + + +

Hello World

+ + \ No newline at end of file diff --git a/src/exercises/23/exercise.js b/src/exercises/23/exercise.js index ce3802aa..c8f46e51 100644 --- a/src/exercises/23/exercise.js +++ b/src/exercises/23/exercise.js @@ -1,17 +1,67 @@ -// Why does a call to the delayPrint() do. -// - Comment your understanding -// - Provide a fix. +// // Why does a call to the delayPrint() do. +// // - Comment your understanding +// // - Provide a fix. +// class Printer { +// constructor() { +// this.index = 0; +// } + +// print() { +// console.log(`Printing sheet number ${this.index}`); +// this.index++; +// } +// delayPrint() { +// console.log("print", this); +// setTimeout(() => { +// this.print(); +// }, 1000); +// console.log(this.index); +// } +// } + +// const p2 = new Printer(); +// p2.delayPrint(); +// // p2.delayPrint(); +// What does a call to the noDelayPrint() do. +// Is 'this' inside print function referrring to an instance of Printer +// Is the correct sheet printed? +// Comment your understanding +// Provide a fix. + +//this is commenting to the printer object +//yes the current sheet is printed +//if there two functions with the same the one which comes second +//the code in second one will be executed +// we should the function inside the arrow function +//or else this will be binded to the global window class Printer { - constructor(index) { + constructor() { this.index = 0; } print() { console.log(`Printing sheet number ${this.index}`); + } + + noDelayPrint() { + console.log(`1 You asked me to print the sheet ${this.index}`); + setTimeout(() => { + this.print(); + }, 0); // 0 milli-sec delay this.index++; } - delayPrint() { - setTimeout(this.print, 1000); + noDelayPrint() { + console.log(`2 You asked me to print the sheet ${this.index}`); + setTimeout(() => { + this.print(); + }, 1000); //1 sec dely + this.index++; } } +const p = new Printer(); +// console.log(p); +p.print(); +p.noDelayPrint(); +p.noDelayPrint(); +p.noDelayPrint(); diff --git a/src/exercises/23/index.html b/src/exercises/23/index.html new file mode 100644 index 00000000..6a644f58 --- /dev/null +++ b/src/exercises/23/index.html @@ -0,0 +1,9 @@ + + + + + + +

Hello

+ + \ No newline at end of file diff --git a/src/exercises/25/exercise.js b/src/exercises/25/exercise.js index 294b8b6b..a299c8af 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 +// closure it is not explicitly implemented like arrays or objects +//closure has access to the variable environment of Execution context in which it is created +//closures happen internally + +const applyLeaves = function () { + leaves = 0; + return function () { + leaves++; + console.log(`${leaves} leaves count`); + }; +}; +const appliedLeaves = applyLeaves(); +appliedLeaves(); +appliedLeaves(); +//we have call applyleaves then it will be sent into the call stack and be popped out +//then Execution context for appliedLeaves() will be created the varibale environment will be kept +//empty using concept of closure it will access o variable environment of its parent and +//it will return the values diff --git a/src/exercises/26/exercise.js b/src/exercises/26/exercise.js index b886e948..35af2372 100644 --- a/src/exercises/26/exercise.js +++ b/src/exercises/26/exercise.js @@ -6,5 +6,33 @@ // - What are higher order functions? const counter = (function () { - //Your code goes here. + let value = 0; + return [ + function increment() { + value++; + }, + function decrement() { + value--; + }, + function getValue() { + console.log(value); + }, + //Your code goes here. + ]; })(); + +const result = counter; +result[0](); +result[0](); +result[0](); +console.log(result[0]()); +//anonymus function is used since the name is ot given to function +//but it is assigned to a constant counter is an anonymous function + +//IIFE means Immediately Invoked Function Expression.This can we called using anonymous function +//or arrow function no need to assign function sincle it will be called once or used only once and +//cannot be used any where but one thing to remember it should be enclosed in an parenthesis() +// and parenthesis() should be attached at the end to make the call +//Counter is an IIFE + +//higher order function whose parameters may be function or can return another function diff --git a/src/exercises/27/exercise.js b/src/exercises/27/exercise.js index 41ddb7f7..86297d73 100644 --- a/src/exercises/27/exercise.js +++ b/src/exercises/27/exercise.js @@ -1,4 +1,5 @@ // What is a callback? +//callback function is a function which is called inside some other function function welcome(name) { setTimeout(() => { @@ -7,10 +8,12 @@ function welcome(name) { } function success() { - console.log("Greetings successful!!"); + setTimeout(() => { + console.log("Greetings successful!!"); + }, 1001); } -welcome(); +welcome("Bhavana"); success(); // In the above example: diff --git a/src/exercises/28/exercise.js b/src/exercises/28/exercise.js index 37050f14..7f1a3ae8 100644 --- a/src/exercises/28/exercise.js +++ b/src/exercises/28/exercise.js @@ -11,6 +11,17 @@ function learnEventLoops() { setTimeout(function print() { console.log("75% Learning done!"); }, 1); // 1 millisec - console.log("Learning completed!!"); + setTimeout(() => { + console.log("Learning completed!!"); + }, 2); } -main(); +// main(); +learnEventLoops(); +//Event loops is an essential piece that makes asynchronous behaviour possible All the +//async tasks will be running in the wb API environment +//Event loop looks into the call stack and detrmines it is empty or not +//Event loop takes the function fromthe callback queue and put them in callstack + +//the reason for the wrong sequence is it first print "Learning event loop then for function print" +//50% learning done it will be printed after 0 millisec learning didn't have any waiting time +//so it is printed immediately to correct the order put the function inside the setTimeout diff --git a/src/exercises/29/exercise.js b/src/exercises/29/exercise.js index e841af66..8340aeb3 100644 --- a/src/exercises/29/exercise.js +++ b/src/exercises/29/exercise.js @@ -4,3 +4,17 @@ function calculateVolume(length, breadth, height) { return length * breadth * height; } +//currying is used for partial implementation of the arguments +//currying is tranformation of function which translates the function from f(a,b,c) +//f(a)(b)(c) + +function calculateVolume2(length) { + return function (breadth) { + return function (height) { + return length * breadth * height; + }; + }; +} +console.log(calculateVolume2(3)(4)(5)); +//we can create a seperate function for every argument; +// /if the length of the cube is fixed then we can create a funvtion and use it by just changing bredth and width diff --git a/src/exercises/3/exercise.js b/src/exercises/3/exercise.js index ddfc8767..534acb1d 100644 --- a/src/exercises/3/exercise.js +++ b/src/exercises/3/exercise.js @@ -5,10 +5,15 @@ function checkIfArray(input) { let isInputAnArray = false; //... - - if (isInputAnArray) { - console.log("Given input is an Array"); - } else { - console.log("Given input is not an Array"); - } + isInputAnArray = Array.isArray(input); + isInputAnArray + ? console.log("Given input is an Array") + : console.log("Given input is not an Array"); + //used conditional operator + // if (isInputAnArray) { + // console.log("Given input is an Array"); + // } else { + // console.log("Given input is not an Array"); + // } } +checkIfArray([1, 2, 3, 4, 5, 6]); diff --git a/src/exercises/30/exercise.js b/src/exercises/30/exercise.js index 9cf6aa66..9e8dc2a5 100644 --- a/src/exercises/30/exercise.js +++ b/src/exercises/30/exercise.js @@ -3,3 +3,15 @@ // - Stop the timer at exactly 1min // - Display it on a HTML page // - What is the function that you have used? +let t = 0; +console.log("setTime out "); +let id = setInterval(() => { + t++; + if (t == 60) { + clearInterval(id); + } else { + document.getElementById("heading").innerHTML = t; + } +}, 1000); +//the function used it setInterval it executes here for every one second +//also used clearInterval to delete the timer after 60 secs diff --git a/src/exercises/30/index.html b/src/exercises/30/index.html new file mode 100644 index 00000000..2b16cabb --- /dev/null +++ b/src/exercises/30/index.html @@ -0,0 +1,8 @@ + + + + + +

Time starts now

+ + \ No newline at end of file diff --git a/src/exercises/31/exercise.js b/src/exercises/31/exercise.js index 8409e2ba..9382b322 100644 --- a/src/exercises/31/exercise.js +++ b/src/exercises/31/exercise.js @@ -11,3 +11,34 @@ // Declaration of translate // function translate(sourceString:string, targetLanguage:string) +class Translator { + translate(targetMessage, targetLanguage) { + const encodedParams = new URLSearchParams(); + encodedParams.append("q", targetMessage); + 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": "cb405502e6msh0c7375c09c15a26p118a01jsn421fac85451a", + "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)); + } +} +const languageTranslator = new Translator(); +languageTranslator.translate("Hello", "fr"); diff --git a/src/exercises/31/index.html b/src/exercises/31/index.html new file mode 100644 index 00000000..101fa0f9 --- /dev/null +++ b/src/exercises/31/index.html @@ -0,0 +1,9 @@ + + + + + + +

Hello2

+ + \ No newline at end of file diff --git a/src/exercises/32/exercise.js b/src/exercises/32/exercise.js index 95cb722e..176e2270 100644 --- a/src/exercises/32/exercise.js +++ b/src/exercises/32/exercise.js @@ -8,4 +8,32 @@ // CurrencyConvertor.convert(1,"USD","INR") should return ~83.5 // Declaration of convert -// function convert(amount:number,sourceCurrency:string,targetCurrency:string) +let request = () => { + return new Promise((resolve, reject) => { + const data = null; + + const xhr = new XMLHttpRequest(); + xhr.withCredentials = true; + + xhr.addEventListener("readystatechange", function () { + if (this.readyState === this.DONE) { + console.log(this.responseText); + resolve(this.responseText); + } else { + reject(this.readyState); + } + }); + + xhr.open("GET", "https://airport-info.p.rapidapi.com/airport?iata=hyd"); + xhr.setRequestHeader( + "X-RapidAPI-Key", + "cb405502e6msh0c7375c09c15a26p118a01jsn421fac85451a" + ); + xhr.setRequestHeader("X-RapidAPI-Host", "airport-info.p.rapidapi.com"); + + xhr.send(data); + }); +}; +request().then((data) => { + console.log(JSON.parse(data)); +}); diff --git a/src/exercises/32/index.html b/src/exercises/32/index.html new file mode 100644 index 00000000..6a644f58 --- /dev/null +++ b/src/exercises/32/index.html @@ -0,0 +1,9 @@ + + + + + + +

Hello

+ + \ No newline at end of file diff --git a/src/exercises/33/exercise.js b/src/exercises/33/exercise.js index 1d66757b..3e592c84 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, ...rest } = obj; +console.log(name); +console.log(rest); diff --git a/src/exercises/34/exercise.js b/src/exercises/34/exercise.js index c018d0e3..92b96488 100644 --- a/src/exercises/34/exercise.js +++ b/src/exercises/34/exercise.js @@ -1,9 +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(4, 5)); +console.log(multiply(5)); +console.log(multiply(1)); // 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..a7395978 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()); - } + return employeeRavi?.experience; } - +console.log(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..6ef365ec 100644 --- a/src/exercises/36/exercise.js +++ b/src/exercises/36/exercise.js @@ -21,3 +21,11 @@ console.log(A() ?? C() ?? D()); console.log(B() ?? D()); //Comment the outputs below and reason +//A Was called as it is undefined it will call C() +//C was called as it is null it will call D() +//D was called +//"foo" + +//B was called it will return false and execution will be completed + +//if it is null or undefined it will move further diff --git a/src/exercises/4/exercise.js b/src/exercises/4/exercise.js index d977ce66..e740c136 100644 --- a/src/exercises/4/exercise.js +++ b/src/exercises/4/exercise.js @@ -4,6 +4,10 @@ function greeting(firstName, lastName) { let welcomeGreeting; + welcomeGreeting = `${firstName} ${lastName}`; console.log(welcomeGreeting); } +greeting("Bhavana", "Kondeti"); + +// template literal can be used to used to dispaly the variable values in the text diff --git a/src/exercises/5/exercise.js b/src/exercises/5/exercise.js index 14ea345c..932afcd2 100644 --- a/src/exercises/5/exercise.js +++ b/src/exercises/5/exercise.js @@ -3,8 +3,11 @@ // - Comment the function that you used. function binaryToDecimal(binaryString) { - let decimalValue; + let decimalValue = parseInt(binaryString, 2); console.log( `Decimal for the given binary string ${binaryString} is ${decimalValue}` ); } +binaryToDecimal("1100"); +//used parseInt with the radix set to 2 ; +//parseInt(binaryString, 2); diff --git a/src/exercises/6/exercise.js b/src/exercises/6/exercise.js index a7149958..98de2f9c 100644 --- a/src/exercises/6/exercise.js +++ b/src/exercises/6/exercise.js @@ -1,7 +1,10 @@ // Complete the below function. Find the length of the given array. // - Also validate the input. Accept the input only if its an array. - function findTheLength(inputArray) { - let lengthOfArray; + let lengthOfArray = Array.isArray(inputArray) + ? inputArray.length + : "Not an array"; console.log(`Length of the given input array is ${lengthOfArray}`); } +let inputArray = [1, 2, 3, 4, 5]; +findTheLength(inputArray); diff --git a/src/exercises/7/exercise.js b/src/exercises/7/exercise.js index 8c18d178..8ea75286 100644 --- a/src/exercises/7/exercise.js +++ b/src/exercises/7/exercise.js @@ -2,6 +2,10 @@ // - Use Array methods function commaSeparatedString(inputArray) { + //using join function for this + let joinedString = inputArray.join(","); + console.log(joinedString); // Given an Array like ["firstName","lastName"] // Return a comma separated String like "firstName,lastName" } +commaSeparatedString(["hello", "Bhavana", "kondeti"]); diff --git a/src/exercises/8/exercise.js b/src/exercises/8/exercise.js index 8cfc9aac..e7d287cd 100644 --- a/src/exercises/8/exercise.js +++ b/src/exercises/8/exercise.js @@ -1,4 +1,8 @@ // Complete the below function. // - Print the given Array -function printArray(inputArray) {} +function printArray(inputArray) { + for (input of inputArray) console.log(input); +} + +printArray([1, 2, 3, 4]); diff --git a/src/exercises/9/exercise.js b/src/exercises/9/exercise.js index d63979cf..ee971051 100644 --- a/src/exercises/9/exercise.js +++ b/src/exercises/9/exercise.js @@ -2,6 +2,15 @@ // - Use Array methods. // - Do not use loops +//Here we will be using map method which it returns an new array with modified elemts in the result array +//map method is simialr to forEach where forEach will not return any array as a result function multiplyArrayByTwo(inputArray) { - console.log(`Given input array is ${inputArray}`); + // inputArray = inputArray.map(function (value) { + // return value * 2; + // }); + + //using array function in the map methods + let updatedArray = inputArray.map((value) => value * 2); + console.log(`Given input array is ${updatedArray}`); } +multiplyArrayByTwo([1, 2, 34, 4]);