From 01440f7bf0a71c2f55c434828edb4192e61e4460 Mon Sep 17 00:00:00 2001 From: siveromar <109865820+siveromar@users.noreply.github.com> Date: Sat, 25 Mar 2023 02:24:17 +0000 Subject: [PATCH] js 3 --- 1-exercises/A-undefined/exercise.js | 11 +- 1-exercises/B-array-literals/exercise.js | 5 +- 1-exercises/C-array-get-set/exercise.js | 5 +- 1-exercises/C-array-get-set/exercises2.js | 3 +- 1-exercises/D-for-loop/exercise.js | 3 + .../E-while-loop-with-array/exercise.js | 8 +- 2-mandatory/1-weather-report.js | 45 ++-- 2-mandatory/2-financial-times.js | 38 ++- 2-mandatory/3-stocks.js | 33 +++ 2-mandatory/index.js | 247 ++++++++++++++++++ util/.vscode/launch.json | 17 ++ 11 files changed, 379 insertions(+), 36 deletions(-) create mode 100644 2-mandatory/index.js create mode 100644 util/.vscode/launch.json diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..6ef6e281 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,25 +12,28 @@ // Example 1 let a; console.log(a); - +//let does not have any value // Example 2 function sayHello() { let message = "Hello"; +// sayHello inside is empty,doesnt return any value } let hello = sayHello(); console.log(hello); - +// // Example 3 -function sayHelloToUser(user) { +function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); //this function is called but no parameter has been passed +//so the variable user has no value // Example 4 let arr = [1,2,3]; console.log(arr[3]); +//3 is not an item of arr diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..10d2432c 100644 --- a/1-exercises/B-array-literals/exercise.js +++ b/1-exercises/B-array-literals/exercise.js @@ -4,8 +4,9 @@ Declare some variables assigned to arrays of values */ -let numbers = []; // add numbers from 1 to 10 into this array -let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares +let numbers = [1,2,3,4,5,6,7,8,9,10]; // add numbers from 1 to 10 into this array +let mentors =["Daniel","Irina" , "Rares" +]; // Create an array with the names of the mentors: Daniel, Irina and Rares /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/C-array-get-set/exercise.js b/1-exercises/C-array-get-set/exercise.js index 5ca911d5..8455eb15 100644 --- a/1-exercises/C-array-get-set/exercise.js +++ b/1-exercises/C-array-get-set/exercise.js @@ -5,11 +5,11 @@ */ function first(arr) { - return; // complete this statement + return arr[0]; // complete this statement } function last(arr) { - return; // complete this statement + return arr[arr.length-1]; // complete this statement } /* @@ -21,6 +21,7 @@ let names = ["Irina", "Ashleigh", "Mozafar", "Joe"]; console.log(first(numbers)); console.log(last(numbers)); console.log(last(names)); +console.log(first(names)); /* EXPECTED RESULT diff --git a/1-exercises/C-array-get-set/exercises2.js b/1-exercises/C-array-get-set/exercises2.js index 6b6b007a..848e713e 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -7,7 +7,8 @@ */ let numbers = [1, 2, 3]; // Don't change this array literal declaration - +numbers[0]=1; +numbers.push(4); /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/1-exercises/D-for-loop/exercise.js b/1-exercises/D-for-loop/exercise.js index 081002b2..5df2c769 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -25,6 +25,9 @@ const AGES = [ 63, 49 ]; +for(let i =0; i { - let usersCities = [ - "London", - "Paris", - "São Paulo" - ] + let usersCities = ["London", "Paris", "São Paulo"]; + expect(getTemperatureReport(usersCities)).toEqual([ "The temperature in London is 10 degrees", @@ -47,10 +49,7 @@ test("should return a temperature report for the user's cities", () => { }); test("should return a temperature report for the user's cities (alternate input)", () => { - let usersCities = [ - "Barcelona", - "Dubai" - ] + let usersCities = ["Barcelona", "Dubai"]; expect(getTemperatureReport(usersCities)).toEqual([ "The temperature in Barcelona is 17 degrees", @@ -60,4 +59,4 @@ test("should return a temperature report for the user's cities (alternate input) test("should return an empty array if the user hasn't selected any cities", () => { expect(getTemperatureReport([])).toEqual([]); -}); \ No newline at end of file +}); diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..b293375c 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,16 +5,30 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let returnedArray = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 65) { + returnedArray.push(allArticleTitles[i]); + } + } + return returnedArray; } - +// npm test -- --testPathPattern 2-financial-times /* The editor of the FT likes short headlines with only a few words! Implement the function below, which returns the title with the fewest words. (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let min = Infinity; + let tempIndex; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length < min) { + min = allArticleTitles[i].length; + tempIndex = i; + } + } + return allArticleTitles[tempIndex]; } /* @@ -24,6 +38,18 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + let headlinesWithNumber = []; + + for (let i = 0; i < allArticleTitles.length; i++) { + for (let j = 0; j < allArticleTitles[i].length; j++) { + if (!isNaN(parseInt(allArticleTitles[i][j]))) { + headlinesWithNumber.push(allArticleTitles[i]); + break; + } + } + } + return headlinesWithNumber; + } /* @@ -32,6 +58,12 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let totalChars = 0; + for (let i = 0; i < allArticleTitles.length; i++) { + totalChars += allArticleTitles[i].length; + } + let avgChars = Math.round(totalChars / allArticleTitles.length); + return avgChars; } diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..f32b5554 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -35,6 +35,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let newAr = []; + for (const element of closingPricesForAllStocks) { + let sum = 0; + let average = 0; + for (let i = 0; i < element.length; i++) { + sum += element[i]; + } + average = sum / element.length; + newAr.push(Math.round(average * 100) / 100); + } + + return newAr; } /* @@ -49,6 +61,13 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + let returnedArray = []; + for (let element of closingPricesForAllStocks) { + let [...newArr] = element; + let changes = newArr[newArr.length - 1] - newArr[0]; + returnedArray.push(Math.round(changes * 100) / 100); + } + return returnedArray; } /* @@ -65,6 +84,20 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + const hightestPrice=[]; + let maxPrice=0; + for(let k=0; k output: 10 +// !console.log(temp); + +// !// +// ! maybe at the future we need to use temp and do some thing , so we need to store it in some variable! +// !temp = temp +30; +// !console.log(temp); +//TODO: third thing you need to do is start coding! + +function temperatureService(city) { + let temparatureMap = new Map(); + + temparatureMap.set("London", 10); + temparatureMap.set("Paris", 12); + temparatureMap.set("Barcelona", 17); + temparatureMap.set("Dubai", 27); + temparatureMap.set("Mumbai", 29); + temparatureMap.set("São Paulo", 23); + temparatureMap.set("Lagos", 33); + + return temparatureMap.get(city); +} + +function getTemperatureReport(cities) { // defining a function + let results = []; + cities.forEach((city) => { + let thisCityTemp = temperatureService(city); + results.push(`The temperature in ${city} is ${thisCityTemp} degrees`); + }); + // for change dynamically : ${} + backTick `` + // result is ready! + return results; +} + +// call functions: + +let testCity = ["Paris", "Dubai"]; +console.log(getTemperatureReport(testCity)); +console.log(" this time we don't select city!"); +console.log(getTemperatureReport([])); // we expect results array as empty array! + +// let myCities = ["tehran","soleimanie"]; +// getTemperatureReport(myCities); /// cities : is same mycities + +// Trace : + +/* +line 61: testCity = ["Paris", "Dubai"] +line 62: getTemeratureReort( parameter : testCity) => getTemeratureReort(["Paris", "Dubai"]) => JS is jump up to line 48! +line 48: Js is looking at what getTemperatureReport does with cities parameter! => JS is going down to line 49! +line 49: results = [] empty array! => JS is going down to line 50! +line 50: JS has to do a foreach on parameter of getTemeperatureRepot call! which was in line 62! => parameter:testCity:["Paris", "Dubai"] + +! JS want to show you what will happen with forEach in line 50: +!["Paris", "Dubai"].forEach( (city) => { +line 51 +!} + +!) +!=> +!paris => {JS will run every lines in foreach for paris} + + +! Hint : JS has run line 50 and is going to run line 51! +line 51: let thisCityTemp = temperatureService(city); +! paris : city => thisCityTemp = temperatureService(paris) ! It's call of new function => which was in line 34! +! line 34 to line 46 is running here: tempetureService(paris) : 12 => thisCityTemp is 12 => +line 52: results.push(`The temperature in ${city} is ${thisCityTemp} degrees`); +! results was empty array! => [].push(`The temperature in ${city} is ${thisCityTemp} degrees`) => city:paris, thisCityTemp:12 +! results will be : ["the temperature in paris is 12 degrees"] + +!Dubai => {JS will run every lines in foreach for Dubai} + + +! Hint : JS has run line 50 and is going to run line 51! +line 51: let thisCityTemp = temperatureService(city); +! Dubai : city => thisCityTemp = temperatureService(Dubai) ! It's call of new function => which was in line 34! +! line 34 to line 46 is running here: tempetureService(Dubai) : 27 => thisCityTemp is 27 => +line 52: results.push(`The temperature in ${city} is ${thisCityTemp} degrees`); +! results was empty array! => ["the temperature in paris is 27 degrees"].push(`The temperature in ${city} is ${thisCityTemp} degrees`) => city:Dubai, thisCityTemp:12 +! results will be : ["the temperature in paris is 12 degrees","the temperature in Dubai is 27 degrees"] + +? now lines 50 to 53 [hint: the same foreach] is ran! and lines 54 and 55 is comments ! so JS will go to run line 56. + +line 56: return results : JS will go to it's memory to find what was result! +! ["the temperature in paris is 12 degrees","the temperature in Dubai is 27 degrees"] + +line 57: } // OH JS will understand running getTemperetureReport function is finished! => +? so JS will comeback to the line where this function was called! +? look at line 73 to find it ! it was called in line 62 => so now JS is going to run line 62: + +! Now JS just will print the result! [ hint: because it want to run console.log() to print output of getTemperetureReport function ] +! It will print ["the temperature in paris is 12 degrees","the temperature in Dubai is 27 degrees"]] +line 63: console.log(" this time we don't select city!"); +! It will print : this time we don't select city! +line 64: console.log(getTemperatureReport([])); +! JS needs to run getTemperatureReport([]) and after that it will print the result! +! JS will jump to where getTemperatureReport() is defined! => it was in line: 48 + +line 48: function getTemperatureReport(cities) { + ! it is just defining this function => JS will go to line 49: + ! cities the input of this function will be [] +line 49: let results = []; +! results = []; +line 50: cities.forEach((city) => { + ! JS will run [].forEach((city) => {} + ! because [].forEach(){} does'nt make sense = > so it will jump to after foreach finished that was in line 53! => + ! Hint: so result is not changed and still it is empty array. + ? lines 54 and 55 are comments! so it will go to line 56: +line 56: return results; => +!an empty array will returned => line 57 : +line 57: } => +! JS will find that this function is finished! +! what will happen? +! JS will jump back to the line which this function was called! +! JS will come back in line 64 : console.log([]); +! JS will print []. => jS is going to line 65! => +line 65 ! JS will find nothing to run! + +TODO : Congradulation you just finished what computer did in less than one second in hours! + + + + + + + +*/ + + + + + + + + + + + + + + +// + +// Array = { +// let length : this, +// forEach(){ + +// }, +// map(){ + +// }, +// push(){ + +// }, +// filter(){ + +// } + +// } + +// numbers = [10, 20, 30, 14, 10]; +// // = [0, 1 ,2, 3, 4]; +// let sum = 0; +// numbers.forEach((num) => { +// sum = sum + num; +// console.log(sum); +// }); + +// use Ctrl + D to change all similar names! + +/* +numbers = [10, 20, 30, 14, 10] +sum = 0 + +num : 10 => 0 + 10 = 10 +num : 20 => 10 + 20 = 30 +num : 30 +num : 14 +num : 10 => 74 + 10 = 84 + + + + + +*/ + +/* +arrayName.foreach( + (parameters)=>{ + do something with parameters + + } +) + + +*/ + +// function addNum(parameters){ +// // what this function do +// } + +// (parameters) => { what this function do!} + +// add +// let i = 0; +// let sum = 0; +// while (i < numbers.length) { +// sum = sum + numbers[i]; +// console.log(sum); +// i +=1; +// } + +/* Trace: +i+=1 : i++ : i=i+1 +i = 0 => sum = 0 , true (i numbers[i]: numbers[0]: 1 => sum = 0 +1 =1 =>next turn: +i = 1 => sum = 1 , true .. : (1<5) => numbers[i]: numbers[1]: 2 => sum = 1 + 2 =3 =>next turn: +i = 2 => sum = 3 , true : (2<5) => numbers[i]: numbers[2]: 3 => sum = 3 + 3 = 6=>... + + + +i = 6 +*/ diff --git a/util/.vscode/launch.json b/util/.vscode/launch.json new file mode 100644 index 00000000..fa030a77 --- /dev/null +++ b/util/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/1-exercises/D-for-loop/exercise.js" + } + ] +} \ No newline at end of file