diff --git a/.vscode/settings.json b/.vscode/settings.json index 8eb2651..69066d1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,6 @@ "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, - "editor.formatOnSave": true + "editor.formatOnSave": true, + "terminal.integrated.sendKeybindingsToShell": true } diff --git a/src/00-hello-world.ts b/src/00-hello-world.ts index b516ceb..cdbaa7d 100644 --- a/src/00-hello-world.ts +++ b/src/00-hello-world.ts @@ -12,7 +12,7 @@ export {}; */ const helloWorld = function() { - return ""; + return "Hello World"; }; -console.log(); // Expected output: "Hello World" +console.log(helloWorld()); // Expected output: "Hello World" diff --git a/src/01-syntax-error.ts b/src/01-syntax-error.ts index bb34c60..08b7d3b 100644 --- a/src/01-syntax-error.ts +++ b/src/01-syntax-error.ts @@ -1,8 +1,13 @@ export {}; -function addNumbers(a b c) { +function addNumbers(a: number, b: number, c: number) { return a + b + c; } const result = addNumbers(1, 3, 4); +const result1 = addNumbers(2, 3, 5); +const result2 = addNumbers(1, 3, 888); + console.log(result); // Expected output: 8 +console.log(result1); +console.log(result2); diff --git a/src/02-syntax-error.ts b/src/02-syntax-error.ts index e18d89c..658f029 100644 --- a/src/02-syntax-error.ts +++ b/src/02-syntax-error.ts @@ -1,7 +1,7 @@ export {}; -function addNumbers(a, b, c) - a + b + c; - +function addNumbers(a:number, b: number, c: number){ + return a + b + c; +} const result = addNumbers(1, 3, 4); console.log(result); // Expected output: 8 diff --git a/src/03-syntax-error.ts b/src/03-syntax-error.ts index f7f565e..373e3ce 100644 --- a/src/03-syntax-error.ts +++ b/src/03-syntax-error.ts @@ -1,6 +1,6 @@ export {}; -function(a, b, c) { +function addNumbers (a:number, b: number, c: number) { return a + b + c; } diff --git a/src/04-logic-error.ts b/src/04-logic-error.ts index 29a4b64..3bddcd7 100644 --- a/src/04-logic-error.ts +++ b/src/04-logic-error.ts @@ -1,7 +1,7 @@ export {}; -function trimWord(word) { - return wordtrim(); +function trimWord(word: string):string { + return word.trim(); } const result = trimWord(" CODELEX "); diff --git a/src/05-boolean.ts b/src/05-boolean.ts index c597246..26dc676 100644 --- a/src/05-boolean.ts +++ b/src/05-boolean.ts @@ -1,11 +1,15 @@ export {}; -const isHappy = "true"; +const isHappy = true; -if (isHappy == false) { +if (isHappy) { console.log("I am happy"); } else { console.log("I am not happy"); } -// Expected output: "I am happy" +// Expected output: "I am happy + +//2. isHappy ? console.log("I am happy") : console.log("I am not happy"); + +//3. console.log(`I am ${isHappy ? '' : 'not' }happy`) diff --git a/src/06-logic-error.ts b/src/06-logic-error.ts index 69a4840..d0dc789 100644 --- a/src/06-logic-error.ts +++ b/src/06-logic-error.ts @@ -1,7 +1,7 @@ export {}; -function trimWord(word) { - return word.trim; +function trimWord(word: string): string{ + return word.trim(); } const result = trimWord(" CODELEX "); diff --git a/src/07-logic-error.ts b/src/07-logic-error.ts index 30f8ccf..a21bd36 100644 --- a/src/07-logic-error.ts +++ b/src/07-logic-error.ts @@ -1,7 +1,7 @@ export {}; -function trim(word) { - return "word".trim(); +function trim(word: string): string { + return word.trim(); } const result = trim("CODELEX "); diff --git a/src/08-function-output.ts b/src/08-function-output.ts index ffe1717..f20ee0c 100644 --- a/src/08-function-output.ts +++ b/src/08-function-output.ts @@ -1,8 +1,8 @@ export {}; -function getNumber() { - return Math.random() * 10; +function getRandomNumber() { + return Math.round(Math.random() * 10); } -const result = null; // call the function created above and put the result inside the variable +const result = getRandomNumber(); // call the function created above and put the result inside the variable console.log(result); // Expected output: {random number} diff --git a/src/09-logic-error.ts b/src/09-logic-error.ts index fdb0169..99d6c58 100644 --- a/src/09-logic-error.ts +++ b/src/09-logic-error.ts @@ -1,8 +1,8 @@ export {}; -function multiply(a, b, c) { - a * b * c; - return; +function multiply(a: number, b: number, c: number): number { + return a * b * c; + } const result = multiply(1, 3, 4); diff --git a/src/10-function-output.ts b/src/10-function-output.ts index d65dc24..8afb1c1 100644 --- a/src/10-function-output.ts +++ b/src/10-function-output.ts @@ -1,8 +1,9 @@ export {}; -function s(w1, w2) { - return w1.concat(' ').concat(w2); +function joinString(w1: string, w2: string): string { + //return w1.concat(' '). concat(w2); + return w1.concat(" ", w2); } -const result = undefined; // concatenate two strings - 'hello', 'world', using the function above +const result = joinString ('hello','world'); // concatenate two strings - 'hello', 'world', using the function above console.log(result); // Expected output: "hello world" diff --git a/src/11-function-output.ts b/src/11-function-output.ts index 3c783dd..159ac0e 100644 --- a/src/11-function-output.ts +++ b/src/11-function-output.ts @@ -1,6 +1,12 @@ export {}; -function concatenate(firstWord, secondWord, thirdWord) { +function concatenate( + firstWord: string, + secondWord: string, + thirdWord: string): + string { + + return `${firstWord} ${secondWord} ${thirdWord}`; // Write the body of this function to concatenate three words together with space between them } diff --git a/src/12-truncate-string.ts b/src/12-truncate-string.ts index 1ada061..3bf5723 100644 --- a/src/12-truncate-string.ts +++ b/src/12-truncate-string.ts @@ -1,3 +1,31 @@ export {}; -console.log(truncateString("CODELEX", 4)); // Expected output: CODE +function truncateString(str: string, length: number):string{ + //return str.substring(0,length) + + + // Expected output: CODE + + + let result = ""; + + for(let i = 0; i < length; i++){ + console.log('i, str[i]:', i, str[i]); + + result = result + str[i]; + console.log('result: ', result); + console.log('======================='); + } + return result; + + } + console.log (truncateString ("CODELEX", 4)); + + + + + + + + + \ No newline at end of file diff --git a/src/13-is-blank.ts b/src/13-is-blank.ts index 92d00d2..d493b29 100644 --- a/src/13-is-blank.ts +++ b/src/13-is-blank.ts @@ -1,8 +1,18 @@ export {}; -/** - * Create a function called isBlank, which checks if passed string is blank or not - */ + + // Create a function called isBlank, which checks if passed string is blank or not + + + function isBlank(value: string | null): boolean { + if(value === null) { + return true; + } + if (value.trim() === ""){ + return true; + } + return false; +} console.log(isBlank(null)); // Expected output: true console.log(isBlank("")); // Expected output: true diff --git a/src/14-repeat-string.ts b/src/14-repeat-string.ts index 9bb46e7..5c7fd0d 100644 --- a/src/14-repeat-string.ts +++ b/src/14-repeat-string.ts @@ -1,6 +1,13 @@ export {}; -const repeatString = () => {}; +const repeatString = (str: string, timesToRepeat: number): string => { + // return str.repeat(timesToRepeat); + let result = str; + for(let i = 1; i < timesToRepeat; i++){ + result += str; //result = result + str; + } + return result; +}; console.log(repeatString("a", 4)); // Expected output: 'aaaa' -console.log(repeatString("b", 5)); // Expected output: 'bbbbb' +console.log(repeatString("bob", 5)); // Expected output: 'bbbbb' diff --git a/src/15-greatest-number.ts b/src/15-greatest-number.ts index 68cedbb..1801ec7 100644 --- a/src/15-greatest-number.ts +++ b/src/15-greatest-number.ts @@ -1,6 +1,8 @@ export {}; -function greatest(x, y) {} +function greatest(x: number, y: number) { + +} console.log(greatest(1, 2)); // Expected output: 2 console.log(greatest(5, 2)); // Expected output: 5 diff --git a/src/16-tax.ts b/src/16-tax.ts index 0938535..dd62ce4 100644 --- a/src/16-tax.ts +++ b/src/16-tax.ts @@ -13,7 +13,9 @@ export {}; */ // You are allowed to change only this function -function calculateSalesTax() {} +function calculateSalesTax(price: number): string { + return (price * 0.21).toFixed(2); +} const product = "You don't know JS"; const price = 19.99; diff --git a/src/17-format-currency.ts b/src/17-format-currency.ts index e172c35..082792f 100644 --- a/src/17-format-currency.ts +++ b/src/17-format-currency.ts @@ -9,8 +9,11 @@ export {}; */ // You are allowed to change only this function -function formatCurrency() {} +function formatCurrency(num: number): string{ + return num.toFixed(2) +} + function calculateSalesTax(price: number) { return price * 0.21; } diff --git a/src/18-currency-conversion.ts b/src/18-currency-conversion.ts index 0b789ab..ad597a2 100644 --- a/src/18-currency-conversion.ts +++ b/src/18-currency-conversion.ts @@ -8,11 +8,20 @@ export {}; */ // You are allowed to change only this function -function convertToUSD() {} +/* +const CONVERSION_RATE_EUR_USD = 1.09; + +function convertToUSD() { + + } + function addFee(price: number): number{ + return price + price * CONVERSION_FEE; + } + const product = "You don't know JS"; const price = 19.99; -const priceInUSD = convertToUSD(price); +const priceInUSD = convertToUSD(price ); console.log("Product: " + product); console.log("Price: $" + priceInUSD); diff --git a/src/19-currency-conversion.ts b/src/19-currency-conversion.ts index 1d057d9..93aee62 100644 --- a/src/19-currency-conversion.ts +++ b/src/19-currency-conversion.ts @@ -15,14 +15,29 @@ export {}; */ // You are allowed to change this function -function convertToUSD(price) {} + +const CONVERSION_FEE = 0.02; +const CONVERSION_RATE_EUR_USD = 1.09; +const CONVERSION_RATE_EUR_BRL = 5.6; +const CONVERSION_RATE_EUR_YEN = 155.6; + +function formatCurrency(num: number): string{ + return num.toFixed(2); +} +function addFee(price: number): number{ + return price + price * CONVERSION_FEE; +} +function convertCurrency(price: number,rate: number): number { + return price * rate; +} // You are allowed to change this function -function convertToBRL(price) {} + const product = "You don't know JS"; const price = 12.5; -const priceInUSD = convertToUSD(price); -const priceInBRL = convertToBRL(price); +const convertedToUSD = convertCurrency(price, CONVERSION_RATE_EUR_USD); +const priceInUSD = formatCurrency(addFee(convertedToUSD)); +const priceInBRL = formatCurrency(addFee(convertCurrency(price, CONVERSION_RATE_EUR_BRL))); console.log("Product: " + product); console.log("Price: $" + priceInUSD); diff --git a/src/20-arrays.ts b/src/20-arrays.ts index 4ad8c88..ece6d4c 100644 --- a/src/20-arrays.ts +++ b/src/20-arrays.ts @@ -8,4 +8,9 @@ export {}; * - https://javascript.info/array */ -console.log(stringToArray(["John Doe"])); // Expected output: ['John', 'Doe'] +function stringToArray(fullName: string): string[] { + return fullName.split(" "); +} + +const nameArray = stringToArray("John Doe"); +console.log(nameArray); // Expected output: ['John', 'Doe'] diff --git a/src/21-map.ts b/src/21-map.ts index 5e5f020..a4ab7a2 100644 --- a/src/21-map.ts +++ b/src/21-map.ts @@ -1,16 +1,15 @@ export {}; -function tidyUpString(str) { - return str - .trim() - .toLowerCase() - .replace("/", ""); +function tidyUpString(str: string): string { + return str.trim().toLowerCase().replace("/", ""); } // You are allowed to edit this function -function capitalise(str) {} +function capitalise(str: string): string { + return str.charAt(0).toUpperCase() + str.slice(1); +} const mentors = ["/Daniel ", "irina ", " Gordon", "ashleigh "]; -let mentorsTidy; // You are allowed to edit this line +let mentorsTidy = mentors.map((mentor) => capitalise(tidyUpString(mentor))); // You are allowed to edit this line console.log(mentorsTidy); // Expected output: ["Daniel", "Irina", "Gordon", "Ashleigh"] diff --git a/src/22-sort.ts b/src/22-sort.ts index b5b19a2..27f9357 100644 --- a/src/22-sort.ts +++ b/src/22-sort.ts @@ -1,6 +1,7 @@ export {}; const letters = ["a", "n", "c", "e", "z", "f"]; -let sortedLetters; // You are allowed to change only this line +let sortedLetters = [...letters].sort(); // You are allowed to change only this line console.log(sortedLetters); // Expected output: [ 'a', 'c', 'e', 'f', 'n', 'z' ] + \ No newline at end of file diff --git a/src/23-min-max.ts b/src/23-min-max.ts index 153eaf4..c6ba65b 100644 --- a/src/23-min-max.ts +++ b/src/23-min-max.ts @@ -5,9 +5,29 @@ export {}; * one of which does not use built-in Math methods. */ -const min = array => {}; +const min = (array: any) => { + let min = array[0]; + for (let i = 1; i < array.length; i++) { + if (array[i] < min) { + min = array[i]; + } + } + return min; +}; -const max = array => {}; +const max = (array: any) => { + let max = array[0]; + for (let i = 1; i > max; i++) { + if (array[i] > max) { + max = array[i]; + } + } + return max; +}; +{ + const min = (array: any) => Math.min(...array); + const max = (array: any) => Math.max(...array); +} console.log(min([1, 2, 3, 4, 5])); // Expected output: 1 console.log(min([9, -3, 6])); // Expected output: -3 diff --git a/src/24-looping-with-conditions.ts b/src/24-looping-with-conditions.ts index a4cbfd1..8794355 100644 --- a/src/24-looping-with-conditions.ts +++ b/src/24-looping-with-conditions.ts @@ -1,5 +1,7 @@ export {}; -function onlyTheAces(arr) {} +function onlyTheAces(arr: string[]): string[] { + return arr.filter(element => element === "Ace"); +} console.log(onlyTheAces(["Ace", "King", "Queen", "Jack", "Ace"])); // Expected result: ['Ace', 'Ace'] diff --git a/src/25-calculator.ts b/src/25-calculator.ts index 87e062d..3a75d15 100644 --- a/src/25-calculator.ts +++ b/src/25-calculator.ts @@ -1,14 +1,41 @@ export {}; -function add() {} +function add(a: number, b: number): number { + return a + b; +} -function subtract() {} +function subtract(a: number, b: number): number { + return a - b; +} + +function sum(num: number[]): number { + let sum = 0; + for (let i = 0; i < num.length; i++) { + sum += num[i]; + } + return sum; +} +const result = sum([1,2,3]); + + + + +function multiply(num: number[]): any { + let result = 1; + for (let i = 0; i < num.length; i++){ + result *= num[i]; + } + return result; + +} + +function power(a: number, b: number): number { + return Math.pow(a,b); +} +const output = power(2,3); -function sum() {} -function multiply() {} -function power() {} console.log(add(1, 2)); // Expected output: 3 console.log(subtract(1, 2)); // Expected output: -1 diff --git a/src/26-validation.ts b/src/26-validation.ts index 6b733e6..b1af83a 100644 --- a/src/26-validation.ts +++ b/src/26-validation.ts @@ -11,14 +11,24 @@ export {}; const excludedNums = [6, 14, 91, 111]; // You are allowed to edit only this function -function validate(num) {} +function validate(value: any): any { + return ( + typeof value === "number" && + Number.isInteger(value) && + !excludedNums.includes(value) + ); +} console.log(validate(6)); console.log(validate(10.5)); console.log(validate(101)); console.log(validate(-91)); console.log(validate("16")); - +console.log(validate(true)); //falce) +console.log(validate([4, "34"])); //falce +console.log(validate(null)); //falce +console.log(validate({})); //falce +console.log(validate(() =>{})); //falce /* Expected output: diff --git a/src/27-slice.ts b/src/27-slice.ts index 9d55cc3..91d3fb7 100644 --- a/src/27-slice.ts +++ b/src/27-slice.ts @@ -4,6 +4,9 @@ export {}; * Write function first5 which returns first five elements from the array */ +function first5(arr: number[]): number[] { + return arr.slice(0, 5); +} const numbers = [1, 2, 3, 4, 5, 6, 7, 8]; // You are NOT allowed to edit this line const first5Numbers = first5(numbers); // You are NOT allowed to edit this line diff --git a/src/28-for-loops.ts b/src/28-for-loops.ts index a9c6b2a..669a2a1 100644 --- a/src/28-for-loops.ts +++ b/src/28-for-loops.ts @@ -9,12 +9,21 @@ export {}; * - https://www.youtube.com/watch?v=Kn06785pkJg (JavaScript Loops Made Easy) */ -function goThroughNumbers(start, end) {} +function goThroughNumbers(start: number, end: number): void { + if(start > end) { + console.log("invalid input"); + return; + } + for(let i = start; i <= end; i++){ + console.log(`> ${i} - ${i % 2 === 0 ? 'even' : 'odd'}`); + } + console.log("==============") +} goThroughNumbers(3, 7); /* Expected output: - > 3 - odd + > 3 - odd > 4 - even > 5 - odd > 6 - even diff --git a/src/29-draw-triangle.ts b/src/29-draw-triangle.ts index 994e3e8..d05c85c 100644 --- a/src/29-draw-triangle.ts +++ b/src/29-draw-triangle.ts @@ -5,8 +5,13 @@ export {}; * - https://blog.codeanalogies.com/2017/11/07/javascript-for-loops-explained/ */ -function draw() {} - +function draw(n: number): any { + let i = 0; + i <= 3; + for (let i = 1; i <= 3; i++) { + console.log(" * " + i); + } +} draw(3); /* Expected output: @@ -14,9 +19,16 @@ draw(3); ** *** -*/ +*/ console.log("==================="); -draw(5); +function draw1(n: number): any { + let j = 0; + j <= 5; + for (let j = 1; j <= 5; j++) { + console.log(" * " + j); + } +} +draw1(5); /* Expected output: * diff --git a/src/30-objects.ts b/src/30-objects.ts index b166fc8..1ab1e11 100644 --- a/src/30-objects.ts +++ b/src/30-objects.ts @@ -8,18 +8,28 @@ export {}; * Luckily they're not so difficult to learn. As always w3schools will help: * - https://www.w3schools.com/js/js_objects.asp */ - -const books = [ +type Book = { author?: string; title: string; year?: number; pageCount?: number, publisher?: string }; +const books: Book[] = [ { title: "4 hour work week", author: "Tim Ferris" }, { title: "Tools of Titans", - author: "Tim Ferris" + author: "Tim Ferris", + year: 1998, + pageCount: 309, + publisher: 'Zvaigzne' + }, + { + title: "Tools of Titans", + year: 1998, + pageCount: 309, + publisher: 'Zvaigzne' + } ]; -const getTheTitles = () => {}; +const getTheTitles = (books: Book[]) => books.map((book: Book) => book.title); console.log(getTheTitles(books)); // Expected output: ['4 hour work week', 'Tools of Titans'] diff --git a/src/31-writers.ts b/src/31-writers.ts index 2923e5f..6fa1038 100644 --- a/src/31-writers.ts +++ b/src/31-writers.ts @@ -11,27 +11,34 @@ const writers = [ lastName: "Woolf", occupation: "writer", age: 59, - alive: false + alive: false, }, { firstName: "Zadie", lastName: "Smith", occupation: "writer", age: 41, - alive: true + alive: true, }, { firstName: "Jane", lastName: "Austen", occupation: "writer", age: 41, - alive: false + alive: false, }, { firstName: "bell", lastName: "hooks", occupation: "writer", age: 64, - alive: true - } + alive: true, + }, ]; +writers.forEach((writer) => { + if (writer.alive) { + console.log( + `Hi, my name is ${writer.firstName} ${writer.lastName}.I am ${writer.age} years old, and work as a ${writer.occupation}. ` + ); + } +}); diff --git a/src/32-reading-status.ts b/src/32-reading-status.ts index f64ec18..99e9c34 100644 --- a/src/32-reading-status.ts +++ b/src/32-reading-status.ts @@ -1,24 +1,36 @@ export {}; - -const library = [ +interface Book { + title: string; + author: string; + isRead: boolean; +} +const library: Book[] = [ { title: "The Road Ahead", author: "Bill Gates", - isRead: true + isRead: true, }, { title: "Steve Jobs", author: "Walter Isaacson", - isRead: true + isRead: true, }, { title: "Mockingjay: The Final Book of The Hunger Games", author: "Suzanne Collins", - isRead: false - } + isRead: false, + }, ]; -const showStatus = () => {}; +const showStatus = (library: Book[]) => { + library.forEach((book: Book) => { + if (book.isRead) { + console.log(`Already read '${book.title}' by ${book.author}. `); + } else { + console.log(`You still need to read '${book.title}' by ${book.author}.`); + } + }); +}; showStatus(library); diff --git a/src/33-temperature-conversion.ts b/src/33-temperature-conversion.ts index 180fdfa..e95b6bf 100644 --- a/src/33-temperature-conversion.ts +++ b/src/33-temperature-conversion.ts @@ -14,8 +14,19 @@ export {}; * Temperature should be rounded to one decimal place: i.e., fahrenheitToCelsius(100) should return 37.8 and not 37.77777777777778. */ -const fahrenheitToCelsius = ? -const celsiusToFahrenheit = ? +/*const fahrenheitToCelsius = (fahrenheit: number) => { + return parseFloat((((fahrenheit - 32) * 5) / 9).toFixed(1)); +}; +const celsiusToFahrenheit = (celsius: number) => { + return parseFloat(((celsius * 5) / 9 + 32).toFixed(1)); + ======================================================== */ + +const fahrenheitToCelsius = (fahrenheit: number) => { + return Math.round((((fahrenheit - 32) * 5) / 9) * 10); +}; +const celsiusToFahrenheit = (celsius: number) => { + return Math.round(((celsius * 9) / 5 + 32) * 10) / 10; +}; console.log(fahrenheitToCelsius(32)); // Expected result: 0 console.log(celsiusToFahrenheit(0)); // Expected result: 32 diff --git a/src/34-protect-email.ts b/src/34-protect-email.ts index 64303ae..6d9c0a8 100644 --- a/src/34-protect-email.ts +++ b/src/34-protect-email.ts @@ -3,6 +3,12 @@ export {}; /** * Create a function protectEmail which hides some symbols of the email */ +function protectEmail(email: string) { + const [user, domain] = email.split("@"); + let protectedUser = user.length > 2 ? user.slice(0, Math.min(user.length - 1, 3)) + "..." : "..."; + return `${protectedUser}@${domain}`; + +} console.log(protectEmail("secret123@codelex.io")); // Expected result: sec...@codelex.io console.log(protectEmail("example@example.com")); // Expected result: exa...@example.com diff --git a/src/35-sum-all.ts b/src/35-sum-all.ts index 0c6a145..54462fa 100644 --- a/src/35-sum-all.ts +++ b/src/35-sum-all.ts @@ -6,6 +6,13 @@ export {}; * - 1, 4 will return 1 + 2 + 3 + 4 which is 10 */ -const sumAll = function() {}; +let sumAll = [1, 2, 3, 4]; -console.log(sumAll(1, 4)); // Expected output: 10 +function sumNumbers(array: number[]){ + let sum = 0; + for (let i = 0; i < array.length; i++) { + sum += array[i]; + } + return sum; +} +console.log(sumNumbers(sumAll)); diff --git a/src/36-pythagorean-theorem.ts b/src/36-pythagorean-theorem.ts index 11ebd41..dcfe6ce 100644 --- a/src/36-pythagorean-theorem.ts +++ b/src/36-pythagorean-theorem.ts @@ -10,7 +10,10 @@ export {}; * and name them in your function accordingly. */ -const pythagoreanTheorem = () => {}; +const pythagoreanTheorem = (a: number, b: number): number => { + return Math.sqrt(a * a + b * b); + +}; console.log(pythagoreanTheorem(2, 4)); // Expected result: 4.47213595499958 console.log(pythagoreanTheorem(3, 4)); // Expected result: 5 diff --git a/src/37-indexOf.ts b/src/37-indexOf.ts index 9e71925..48fed72 100644 --- a/src/37-indexOf.ts +++ b/src/37-indexOf.ts @@ -8,7 +8,14 @@ export {}; */ // You are allowed to edit only this function -function remove(arr, valueToRemove) {} +function remove(arr: any, valueToRemove: any) { + const index = arr.indexOf(valueToRemove); + if(index === -1){ + return arr.slice(); + } + return[...arr.slice(0,index), ...arr.slice(index + 1)]; + +} const numbers = [1, 2, 3]; const names = ["John", "Alice", "Ellen"]; diff --git a/src/38-remove-from-array.ts b/src/38-remove-from-array.ts index 10497a0..4bb8791 100644 --- a/src/38-remove-from-array.ts +++ b/src/38-remove-from-array.ts @@ -1,6 +1,11 @@ export {}; -const removeFromArray = function() {}; +//const removeFromArray = function() {}; + +const removeFromArray = function (number: number[], ...restNums: number[]): number[] +{ + return number.filter (number => !restNums.includes(number) ) +}; console.log(removeFromArray([1, 2, 3, 4], 3)); // Expected output: [1, 2, 4] console.log(removeFromArray([1, 2, 3, 4], 7)); // Expected output: [1, 2, 3, 4] diff --git a/src/39-string-occurences.ts b/src/39-string-occurences.ts index 3468820..17c6732 100644 --- a/src/39-string-occurences.ts +++ b/src/39-string-occurences.ts @@ -1,6 +1,10 @@ export {}; -const count = () => {}; +const count = (text: string, word: string) => { + const regex = new RegExp(`\\b${word}\\b`, 'gi'); + const matches = text.match(regex); + return matches ? matches.length : 0; +}; console.log(count("The quick brown fox jumps over the lazy dog", "the")); // Expected output: 2 console.log(count("The quick brown fox jumps over the lazy dog", "fox")); // Expected output: 1 diff --git a/src/40-multiply-map.ts b/src/40-multiply-map.ts index d7f8e6b..0c0f633 100644 --- a/src/40-multiply-map.ts +++ b/src/40-multiply-map.ts @@ -8,10 +8,19 @@ export {}; * and recreate the logic yourself. */ -const map = () => {}; +/*const map = ( + array: number[], + transform: (value: number, index: number, array: number[]) => number +) => { + let result = []; + for (let i = 0; i < array.length; i++) { + result.push(transform(array[i], i, array)); + } + return result; +}; const numbers = [1, 2, 3]; -const doubled = map(numbers, function(number) { +const doubled = map(numbers, function (number) { return number * 2; }); -console.log(doubled); // Expected result: [2, 4, 6] +console.log(doubled); // Expected result: [2, 4, 6]*/