diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/2024s-lab-01-Llyfrs.iml b/.idea/2024s-lab-01-Llyfrs.iml new file mode 100644 index 0000000..24643cc --- /dev/null +++ b/.idea/2024s-lab-01-Llyfrs.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..df994e7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/task-array.js b/task-array.js index 5f82dd0..2cf4b13 100644 --- a/task-array.js +++ b/task-array.js @@ -6,86 +6,119 @@ // Write these functions: // This block generates array with random length with values between 1-100 -export let numbers = []; +let numbers = []; const length = Math.ceil(Math.random() * 10) + 5; for (let i = 0; i < length; i = i + 1) { numbers.push(Math.ceil(Math.random() * 100)); } // a) Function which will print to console a whole array -export const printArray = (numbers) => { - // Your code: +const printArray = (numbers) => { + // Your code: + console.log(numbers); }; // b) Function which will print to console the length of array -export const printLength = (numbers) => { +const printLength = (numbers) => { // Your code: + console.log(numbers.length); }; // c) Function which will print to console the first element of array -export const printFirstItem = (numbers) => { +const printFirstItem = (numbers) => { // Your code: - + console.log(numbers[0]); }; // d) Function which will print to console the last element -export const printLastItem = (numbers) => { +const printLastItem = (numbers) => { // Your code: - + console.log(numbers[numbers.length - 1]); }; // e) Function which will print to console the largest number (You can check Math functions) -export const printLargestItem = (numbers) => { +const printLargestItem = (numbers) => { // Your code: + console.log(Math.max(...numbers)); }; // f) Function which will print to console the smallest number (You can check Math functions) -export const printSmallestItem = (numbers) => { +const printSmallestItem = (numbers) => { // Your code: - + console.log(Math.min(...numbers)); }; // g) Function which will print to console the sum of all numbers in array (You can check reduce function) -export const printSum = (numbers) => { +const printSum = (numbers) => { // Your code: + console.log(numbers.reduce((a, b) => a + b, 0)); }; // h) Function which will print to console the difference between the largest and the smallest number (You can check Math functions) -export const printSALDifference = (numbers) => { +const printSALDifference = (numbers) => { // Your code: + console.log(Math.max(...numbers) - Math.min(...numbers)); }; // i) Function which will print to console the average of all numbers (You can check reduce function) -export const printAverage = (numbers) => { +const printAverage = (numbers) => { // Your code: - + console.log(numbers.reduce((a, b) => a + b, 0) / numbers.length); }; // j) Function which will print to console the index of largest number (You can check Math functions) -export const printLargestsIndex = (numbers) => { +const printLargestsIndex = (numbers) => { // Your code: - + console.log(numbers.indexOf(Math.max(...numbers))); }; // k) Function which will print to console the even numbers (not the array of even numbers), // if array doesn't contain any even number, show text "Even number isn't in array" -export const printEvenNums = (numbers) => { +const printEvenNums = (numbers) => { // Your code: - + let evenNumbers = numbers.filter(number => number % 2 === 0); + console.log(evenNumbers.length > 0 ? evenNumbers : "Even number isn't in array" ); }; // l) Function which will multiple by 2 every number in array and print the array to console // Example: printNumsMultipliedBy2([1,2,3]) -> [2,4,6] -export const printNumsMultipliedBy2 = (numbers) => { +const printNumsMultipliedBy2 = (numbers) => { // Your code: + console.log(numbers.map(number => number * 2)); +}; -}; \ No newline at end of file +// Test all functions + +console.log("Array: "); +printArray(numbers); +console.log("Length: "); +printLength(numbers); +console.log("First item: "); +printFirstItem(numbers); +console.log("Last item: "); +printLastItem(numbers); +console.log("Largest item: "); +printLargestItem(numbers); +console.log("Smallest item: "); +printSmallestItem(numbers); +console.log("Sum: "); +printSum(numbers); +console.log("Difference between largest and smallest: "); +printSALDifference(numbers); +console.log("Average: "); +printAverage(numbers); +console.log("Index of largest: "); +printLargestsIndex(numbers); +console.log("Even numbers: "); +printEvenNums(numbers); +console.log("Numbers multiplied by 2: "); +printNumsMultipliedBy2(numbers); \ No newline at end of file diff --git a/task-bonus.js b/task-bonus.js index f45e7d3..f7e6bd4 100644 --- a/task-bonus.js +++ b/task-bonus.js @@ -7,11 +7,23 @@ // * * * * * // Your code: -export const drawTriangle = (length = 5) => { +const drawTriangle = (length = 5) => { - // ... write code ... + for (let i = 1; i <= length; i++) { + + let str = ""; + + for (let j = 0; j < i; j++) { + str += "* "; + } + console.log(str); + + } + }; +drawTriangle(5); + // 2# ========== BONUS ======================= // Write function which will (with cycles) display this (keep in mind that there is no space after the last char): // * * * * * * * * * * @@ -27,11 +39,14 @@ export const drawTriangle = (length = 5) => { // J A V A S C R I P T // Your code: -export const drawJavascriptWord = (word = "javascript") => { - // ... write code ... +const drawJavascriptWord = (word = "javascript") => { + for (let i = 0; i <= word.length; i++) { + let str = word.slice(word.length - i , word.length); + console.log("*".repeat(word.length - i) + str) + } }; - +drawJavascriptWord(); // 3# ========== BONUS ======================= // Create function that takes array of vehicles with measured top speeds. Return array of vehicle with top speed. // Example: @@ -47,6 +62,18 @@ export const drawJavascriptWord = (word = "javascript") => { // ]; // Your code: -export const getVehiclesAndTopSpeed = (vehicles) => { - -}; \ No newline at end of file +const getVehiclesAndTopSpeed = (vehicles) => { + + for (let vehicle of vehicles) { + vehicle.topSpeed = Math.max(...vehicle.measuredSpeeds); + delete vehicle.measuredSpeeds; + } + + return vehicles; +}; + +console.log(getVehiclesAndTopSpeed([ + { name: "Executor Star Dreadnought", measuredSpeeds: [555, 545, 577, 600] }, + { name: "T-47 Airspeeder", measuredSpeeds: [300, 311, 299, 350] }, + { name: "AT-AT", measuredSpeeds: [20, 21, 20, 19] }, +])); \ No newline at end of file diff --git a/task-cycles.js b/task-cycles.js index 6c47235..308952d 100644 --- a/task-cycles.js +++ b/task-cycles.js @@ -6,10 +6,17 @@ // arrayOfMultiples(17, 6) ➞ [17, 34, 51, 68, 85, 102] // Your code: -export const arrayOfMultiples = (num, length) => { - // ... write code ... +const arrayOfMultiples = (num, length) => { + + return Array.from({length: length}, (_, i) => num * (i + 1)); + }; +console.log("Multiples") +console.log(arrayOfMultiples(7, 5)); // ➞ [7, 14, 21, 28, 35] +console.log(arrayOfMultiples(12, 10)); // ➞ [12, 24, 36, 48, 60, 72, 84, 96, 108, 120] +console.log(arrayOfMultiples(17, 6)); // ➞ [17, 34, 51, 68, 85, 102] + // 2 ================================= // Change direction of array // TIP: Check if there is function which can help you https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array @@ -19,10 +26,15 @@ export const arrayOfMultiples = (num, length) => { // changeDirection([1, 2]) ➞ [2, 1] // Your code: -export const changeDirection = (array) => { -// ... write code ... +const changeDirection = (array) => { + return array.reverse() }; +console.log("Reverse") +console.log(changeDirection([0, 1, 2, 3])); // ➞ [3, 2, 1, 0] +console.log(changeDirection([])); // ➞ [] +console.log(changeDirection([1, 2])); // ➞ [2, 1] + // 3 ================================= // Create function that takes two arrays and return object with two keys - bigger array, sum all numbers // Examples @@ -30,6 +42,13 @@ export const changeDirection = (array) => { // biggerArray([1,2,3], [2,3,4]) ➞ { array: [2,3,4], sum: 9 } // Your code: -export const biggerArray = (array1, array2) => { -// ... write code ... -}; \ No newline at end of file +const biggerArray = (array1, array2) => { + + const sum = (prev, current) => current + prev + return [ { array: array1, sum: array1.reduce(sum, 0) }, { array: array2, sum: array2.reduce(sum, 0) } ].reduce((prev, current) => prev.sum > current.sum ? prev : current) + +}; + +console.log("Bigger array") +console.log(biggerArray([1,2,3,4,5], [50,50])); // ➞ { array: [50,50], sum: 100 } +console.log(biggerArray([1,2,3], [2,3,4])); // ➞ { array: [2,3,4], sum: 9 } diff --git a/task-object.js b/task-object.js index a0b1541..6fad175 100644 --- a/task-object.js +++ b/task-object.js @@ -9,21 +9,35 @@ // volumeOfBox({ width: 2, length: 3, height: 5 }) ➞ 30 // Your code: -export const volumeOfBox = (obj) => { +const volumeOfBox = (obj) => { + + return obj.width * obj.length * obj.height; }; - + + +console.log(volumeOfBox({ width: 2, length: 5, height: 1 })); // ➞ 10 +console.log(volumeOfBox({ width: 4, length: 2, height: 2 })); // ➞ 16 +console.log(volumeOfBox({ width: 2, length: 3, height: 5 })); // ➞ 30 + + // 2 ---- // Create a function that takes strings - firstname, lastname, age, and return object with firstname, lastname, age, yearOfBirth // Examples // personObject("Obi-wan", "Kenobi", "40") ➞ { firstname: "Obi-wan", lastname: "Kenobi", age: 40, yearOfBirth: 1981 } // Your code: -export const personObject = (firstname, lastname, age) => { +const personObject = (firstname, lastname, age) => { + return { firstname: firstname, + lastname: lastname, + age: age, + yearOfBirth: new Date().getFullYear() - age }; }; +console.log(personObject("Obi-wan", "Kenobi", "40")); + // 3 ---- // Create the function that takes an array with objects and returns the sum of people's budgets. // Example @@ -34,10 +48,18 @@ export const personObject = (firstname, lastname, age) => { // ]) ➞ 65700 //Your code: -export const getBudgets = (persons) => { - +const getBudgets = (persons) => { + return persons.reduce((prev, current) => prev + current.budget, 0); }; + +console.log(getBudgets([ + { name: "John", age: 21, budget: 23000 }, + { name: "Steve", age: 32, budget: 40000 }, + { name: "Martin", age: 16, budget: 2700 } +])); + + // 4 ---- // Create function that takes array of cars and sort them by price // Example @@ -45,7 +67,8 @@ export const getBudgets = (persons) => { // sortVehiclesByPrice(vehicles) ➞ [{name: "T-47 Airspeeder", price :5}, {name: "AT-AT", price :20}, {name: "Executor Star Dreadnought", price: 999}] // Your code: -export const sortVehiclesByPrice = (vehicles) => { - +const sortVehiclesByPrice = (vehicles) => { + return vehicles.sort((a, b) => a.price - b.price); +}; -}; \ No newline at end of file +console.log(sortVehiclesByPrice([{name: "Executor Star Dreadnought", price: 999}, {name: "T-47 Airspeeder", price: 5}, {name: "AT-AT", price : 20}])); \ No newline at end of file diff --git a/task-strings.js b/task-strings.js index d1872c9..61de09c 100644 --- a/task-strings.js +++ b/task-strings.js @@ -7,10 +7,12 @@ // "javascript-is-the-best" // Your code: -export const parametrize = (input) => { - +const parametrize = (input) => { + return input.toLowerCase().split(" ").join("-"); }; +console.log( parametrize("Javascript is the best") ); + // 2 ================================= // Write function that takes object and return sentence // Example: @@ -18,16 +20,20 @@ export const parametrize = (input) => { // ➞ Hello Obi-wan from Starwars universe! It is nice to meet you! You look awesome for your 40, young Jedi! // Your code: -export const giveSentenceForPerson = (obj) => { - +const giveSentenceForPerson = (obj) => { + return `Hello ${obj.name} from ${obj.location}! It is nice to meet you! You look awesome for your ${obj.age}, young ${obj.position}!`; }; +console.log( giveSentenceForPerson({name: "Obi-wan", position: "Jedi", age: 40, location: "Starwars universe"}) ); + // 3 ================================= // Write function that takes string and replace all `a`, `e`, `i`, `o` with * // Example: // replace("Hello Javascript") ➞ "H*ll* J*v*scr*pt" // Your code: -export const replace = (input) => { +const replace = (input) => { + return input.replace(/[aeio]/g, "*"); +}; -}; \ No newline at end of file +console.log( replace("Hello Javascript") ); \ No newline at end of file