From 7d3faded0a2adc1db286789530669b8b9584bf41 Mon Sep 17 00:00:00 2001 From: AK-macbook Date: Mon, 10 Mar 2025 13:13:37 +0300 Subject: [PATCH] Implement iteration methods using map and filter --- src/challenge.ts | 12 ++++++++---- src/filtering.ts | 5 ++--- src/mapping+filtering.ts | 4 ++-- src/mapping.ts | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/challenge.ts b/src/challenge.ts index 63a7766..f76d335 100644 --- a/src/challenge.ts +++ b/src/challenge.ts @@ -9,6 +9,9 @@ // logger([1, 2, 3, "one", "two", "three"]); function logger(array: T[]): void { // write your code here... + array.map((element) => { + console.log(element); + }); } // `toCelsius` function that: @@ -20,7 +23,7 @@ function logger(array: T[]): void { function toCelsius(temperatures: number[]): number[] { // write your code here... - return []; // replace empty array with what you see is fit + return temperatures.map((temp) => (temp - 32) * (5 / 9)); // replace empty array with what you see is fit } // `hottestDays` function that: @@ -33,15 +36,16 @@ function toCelsius(temperatures: number[]): number[] { function hottestDays(temperatures: number[], threshold: number): number[] { // write your code here... - return []; // replace empty array with what you see is fit + return temperatures.filter((temp) => temp > threshold); // replace empty array with what you see is fit } // `logHottestDays` function that: // - Accepts "temperatures" parameter of type "number[]", representing temperature values in Fahrenheit. // - Accepts "threshold" parameter of type "number" -// - Log temperatures that exceed the threshold to the console IN DEGREES CELSIUS (hint: you can combine all previous functions) +// - Log temperatures that exceed the threshold to the console IN DEGREES CELSIUS +// (hint: you can combine all previous functions) function logHottestDays(temperatures: number[], threshold: number): void { - // write your code here... + logger(toCelsius(hottestDays(temperatures, threshold))); } export { logger, toCelsius, hottestDays, logHottestDays }; diff --git a/src/filtering.ts b/src/filtering.ts index 49411d8..66f6bb8 100644 --- a/src/filtering.ts +++ b/src/filtering.ts @@ -12,7 +12,7 @@ const numbers = [10, 13, 20, 25, 38, 35, 40]; function greaterThanTwentyFive(numbers: number[]): number[] { // write your code here... - return []; // replace empty array with what you see is fit + return numbers.filter((number) => number >= 25); // replace empty array with what you see is fit } // `divisibleByFive` function that: @@ -22,8 +22,7 @@ function greaterThanTwentyFive(numbers: number[]): number[] { // divisibleByFive(numbers); // => [10, 20, 25, 35, 40] function divisibleByFive(numbers: number[]): number[] { // write your code here... - - return []; // replace empty array with what you see is fit + return numbers.filter((number) => number % 5 == 0); // replace empty array with what you see is fit } export { greaterThanTwentyFive, divisibleByFive }; diff --git a/src/mapping+filtering.ts b/src/mapping+filtering.ts index 269ad83..6c66176 100644 --- a/src/mapping+filtering.ts +++ b/src/mapping+filtering.ts @@ -12,7 +12,7 @@ const numbers = [10, 13, 20, 25, 38, 35, 40]; function filteredAndSquared(numbers: number[]): number[] { // write your code here... - return []; // replace empty array with what you see is fit + return numbers.filter((n) => n >= 20).map((n) => n * n); // replace empty array with what you see is fit } // `filteredAndTripled` function that: @@ -23,7 +23,7 @@ function filteredAndSquared(numbers: number[]): number[] { function filteredAndTripled(numbers: number[]): number[] { // write your code here... - return []; // replace empty array with what you see is fit + return numbers.filter((n) => n % 5 === 0).map((n) => n * 3); // replace empty array with what you see is fit } export { filteredAndSquared, filteredAndTripled }; diff --git a/src/mapping.ts b/src/mapping.ts index 6f18eef..76b0b94 100644 --- a/src/mapping.ts +++ b/src/mapping.ts @@ -12,7 +12,7 @@ const numbers = [10, 13, 20, 25, 38, 35, 40]; function squaredNumbers(numbers: number[]): number[] { // write your code here... - return []; // replace empty array with what you see is fit + return numbers.map((n) => n * n); // replace empty array with what you see is fit } // `doubledNumbers` function that: @@ -23,7 +23,7 @@ function squaredNumbers(numbers: number[]): number[] { function doubledNumbers(numbers: number[]): number[] { // write your code here... - return []; // replace empty array with what you see is fit + return numbers.map((n) => n * 2); // replace empty array with what you see is fit } export { squaredNumbers, doubledNumbers };