Solutions to the exercices
This commit is contained in:
parent
cface9db19
commit
ca0aa10f15
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
const sumAll = function() {
|
||||
const sumAll = function(a,b) {
|
||||
let sumIntegers= a+b;
|
||||
for (let i=a+1; i<b; i++){
|
||||
sumIntegers += i;
|
||||
|
||||
}
|
||||
return sumIntegers;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,15 @@
|
|||
const leapYears = function() {
|
||||
|
||||
const leapYears = function(year) {
|
||||
if (year % 4 === 0) {
|
||||
if (year % 100 === 0) {
|
||||
if (year % 400 === 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = leapYears;
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
const convertToCelsius = function() {
|
||||
const convertToCelsius = function(degress) {
|
||||
const degreesC= (degress-32)*5/9;
|
||||
return Math.round(degreesC*10)/10;
|
||||
|
||||
};
|
||||
|
||||
const convertToFahrenheit = function() {
|
||||
const convertToFahrenheit = function(degrees) {
|
||||
const degreesF= (degrees*9/5)+32;
|
||||
return Math.round(degreesF*10)/10;
|
||||
};
|
||||
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = {
|
||||
convertToCelsius,
|
||||
|
|
|
@ -1,25 +1,43 @@
|
|||
const add = function() {
|
||||
|
||||
const add = function(a,b) {
|
||||
return a+b
|
||||
};
|
||||
|
||||
const subtract = function() {
|
||||
|
||||
const subtract = function(a,b) {
|
||||
return a-b
|
||||
};
|
||||
|
||||
const sum = function() {
|
||||
|
||||
let args=Array.isArray(arguments[0])?arguments[0]:Array.from(arguments)
|
||||
let sum=0
|
||||
for (let i=0;i<args.length;i++){
|
||||
sum+=args[i]
|
||||
}
|
||||
return sum
|
||||
};
|
||||
|
||||
const multiply = function() {
|
||||
let args=Array.isArray(arguments[0])?arguments[0]:Array.from(arguments)
|
||||
let product=1
|
||||
for (let i=0;i<args.length;i++){
|
||||
product*=args[i]
|
||||
}
|
||||
return product
|
||||
|
||||
};
|
||||
|
||||
const power = function() {
|
||||
|
||||
let args=Array.from(arguments)
|
||||
return Math.pow(args[0],args[1])
|
||||
|
||||
};
|
||||
|
||||
const factorial = function() {
|
||||
|
||||
let args= Array.from(arguments)
|
||||
let factorial= 1
|
||||
for (let i=1;i<=args[0];i++){
|
||||
factorial*=i
|
||||
}
|
||||
return factorial
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,4 +1,12 @@
|
|||
const palindromes = function () {
|
||||
const palindromes = function (isPalindrome) {
|
||||
let isPalindromeCleaned = isPalindrome.toLowerCase().replace(/[^a-z]/g, '')
|
||||
let args = Array.isArray(isPalindromeCleaned[0]) ? isPalindromeCleaned[0] : Array.from(arguments)
|
||||
let argsInverted=[]
|
||||
for (let i=args.length-1;i>=0;i--){
|
||||
argsInverted.push(args[i])
|
||||
}
|
||||
|
||||
return true ? args.join('')===argsInverted.join('') : false
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const getTheTitles = function() {
|
||||
|
||||
const getTheTitles = function(books) {
|
||||
return books.map(book => book.title);
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -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];
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue