From ca0aa10f153c8835db303e255d769c4a7cecd5dc Mon Sep 17 00:00:00 2001 From: andram11 Date: Wed, 24 May 2023 18:55:48 +0200 Subject: [PATCH] Solutions to the exercices --- 02_repeatString/repeatString.js | 8 ++++--- 03_reverseString/reverseString.js | 12 ++++++++-- 04_removeFromArray/removeFromArray.js | 8 ++++--- 05_sumAll/sumAll.js | 8 ++++++- 06_leapYears/leapYears.js | 15 ++++++++++--- 07_tempConversion/tempConversion.js | 10 +++++++-- 08_calculator/calculator.js | 32 +++++++++++++++++++++------ 09_palindromes/palindromes.js | 10 ++++++++- 10_fibonacci/fibonacci.js | 7 +++++- 11_getTheTitles/getTheTitles.js | 4 ++-- 12_findTheOldest/findTheOldest.js | 15 ++++++++++++- LICENSE | 21 ++++++++++++++++++ 12 files changed, 124 insertions(+), 26 deletions(-) create mode 100644 LICENSE diff --git a/02_repeatString/repeatString.js b/02_repeatString/repeatString.js index 4359bbe..90839c3 100644 --- a/02_repeatString/repeatString.js +++ b/02_repeatString/repeatString.js @@ -1,6 +1,8 @@ -const repeatString = function() { - -}; +const repeatString = function(stringName, numberOfRepetitions) { + return stringName.repeat(numberOfRepetitions) + + + }; // Do not edit below this line module.exports = repeatString; diff --git a/03_reverseString/reverseString.js b/03_reverseString/reverseString.js index f6790f0..5950dd5 100644 --- a/03_reverseString/reverseString.js +++ b/03_reverseString/reverseString.js @@ -1,6 +1,14 @@ -const reverseString = function() { +const reverseString = function(stringToBeReversed) { + let reversedStringArray = [stringToBeReversed.split("")]; + let reversedString = ""; + + for (let i = reversedStringArray[0].length-1; i >= 0; i--) { + reversedString += reversedStringArray[0][i]; + } + return reversedString; + + }; - // Do not edit below this line module.exports = reverseString; diff --git a/04_removeFromArray/removeFromArray.js b/04_removeFromArray/removeFromArray.js index 1bedeb0..02ee708 100644 --- a/04_removeFromArray/removeFromArray.js +++ b/04_removeFromArray/removeFromArray.js @@ -1,6 +1,8 @@ -const removeFromArray = function() { - -}; +const removeFromArray = function(arrayValues, valuesToRemove) { + const index = arrayValues.indexOf(valuesToRemove); + arrayValues.splice(index, 1); + return arrayValues; + } // Do not edit below this line module.exports = removeFromArray; diff --git a/05_sumAll/sumAll.js b/05_sumAll/sumAll.js index 00880c7..d4526c1 100644 --- a/05_sumAll/sumAll.js +++ b/05_sumAll/sumAll.js @@ -1,4 +1,10 @@ -const sumAll = function() { +const sumAll = function(a,b) { + let sumIntegers= a+b; + for (let i=a+1; i=0;i--){ + argsInverted.push(args[i]) + } + + return true ? args.join('')===argsInverted.join('') : false }; diff --git a/10_fibonacci/fibonacci.js b/10_fibonacci/fibonacci.js index bb2c8cc..b4e20de 100644 --- a/10_fibonacci/fibonacci.js +++ b/10_fibonacci/fibonacci.js @@ -1,5 +1,10 @@ -const fibonacci = function() { +const fibonacci = function(sequenceNumber) { + let sequence = [1, 1]; + for (let i=1; i<=sequenceNumber; i++) { + sequence.push(sequence[i] + sequence[i-1]); + } + return sequence[sequenceNumber-1]; }; // Do not edit below this line diff --git a/11_getTheTitles/getTheTitles.js b/11_getTheTitles/getTheTitles.js index 74b04df..93cfc48 100644 --- a/11_getTheTitles/getTheTitles.js +++ b/11_getTheTitles/getTheTitles.js @@ -1,5 +1,5 @@ -const getTheTitles = function() { - +const getTheTitles = function(books) { + return books.map(book => book.title); }; // Do not edit below this line diff --git a/12_findTheOldest/findTheOldest.js b/12_findTheOldest/findTheOldest.js index 366856a..8c72181 100644 --- a/12_findTheOldest/findTheOldest.js +++ b/12_findTheOldest/findTheOldest.js @@ -1,4 +1,17 @@ -const findTheOldest = function() { +const findTheOldest = function(people) { + + function calculateAge(yearOfBirth, yearOfDeath){ + if (yearOfDeath=== undefined){ + yearOfDeath= new Date().getFullYear(); + + } + return yearOfDeath-yearOfBirth; + } + + const ages= people.map(year => calculateAge(year.yearOfBirth, year.yearOfDeath)); + + const oldest= ages.indexOf(Math.max(...ages)); + return people[oldest]; }; diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9110b16 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 The Odin Project + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.