Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback #1

Open
wants to merge 11 commits into
base: feedback
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/2024s-lab-01-Llyfrs.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 54 additions & 21 deletions task-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};


};
// 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);
43 changes: 35 additions & 8 deletions task-bonus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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):
// * * * * * * * * * *
Expand All @@ -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:
Expand All @@ -47,6 +62,18 @@ export const drawJavascriptWord = (word = "javascript") => {
// ];

// Your code:
export const getVehiclesAndTopSpeed = (vehicles) => {

};
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] },
]));
33 changes: 26 additions & 7 deletions task-cycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,17 +26,29 @@ 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
// biggerArray([1,2,3,4,5], [50,50]) ➞ { array: [50,50], sum: 100 }
// biggerArray([1,2,3], [2,3,4]) ➞ { array: [2,3,4], sum: 9 }

// Your code:
export const biggerArray = (array1, array2) => {
// ... write code ...
};
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 }
39 changes: 31 additions & 8 deletions task-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,18 +48,27 @@ 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
// const vehicles = [{name: "Executor Star Dreadnought", price: 999}, {name: "T-47 Airspeeder", price: 5}, {name: "AT-AT", price : 20}]
// 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);
};

};
console.log(sortVehiclesByPrice([{name: "Executor Star Dreadnought", price: 999}, {name: "T-47 Airspeeder", price: 5}, {name: "AT-AT", price : 20}]));
Loading