Created
December 8, 2020 04:44
-
-
Save dexterbrylle/4d6cd08a6cb24e1ef5ef40da957de149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dynastyReign = [ | |
{ 'San Dynasty': 'MXLI' }, | |
{ 'Viloria Dynasty': 'MCCCIIII' }, | |
{ 'Tan Dynasty': 'MCCCXCVIII' }, | |
{ 'Bon Dynasty': 'MCDXLV' }, | |
{ 'Maiko Dynasty': 'MDCLXIV' }, | |
{ 'Paul Dynasty': 'MCMXLIX' }, | |
{ 'Andre Dynasty': 'MMMXICX' }, | |
]; | |
function longestDynasty (dynastyReign) { | |
if (dynastyReign.length < 1) return 'No Data'; | |
const filtered = dynastyReign | |
.filter(y => convertYear(Object.values(y).toString()) !== null) | |
const fValues = filtered | |
.map(el => convertYear(Object.values(el).toString())) | |
let reign = 0; | |
let max = 0; | |
let temp = 0; | |
for (let i = 1; i < fValues.length; i++) { | |
temp = Math.abs(fValues[i] - fValues[i - 1]) | |
if (temp > max) { | |
max = temp | |
reign = i | |
} | |
} | |
return filtered[reign]; | |
} | |
function convertYear (year) { | |
let y = 0; | |
if (typeof y === 'undefined' || y === null) return y; | |
let regex = new RegExp('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'); | |
if (!regex.test(year)) return null; | |
let romanMap = new Map(); | |
romanMap.set('I', 1); | |
romanMap.set('V', 5); | |
romanMap.set('X', 10); | |
romanMap.set('L', 50); | |
romanMap.set('C', 100); | |
romanMap.set('D', 500); | |
romanMap.set('M', 1000); | |
let l = year.length; | |
for (var i = 0; i < l; i++) { | |
if (romanMap.get(year.charAt(i)) < romanMap.get(year.charAt(i + 1))) { | |
y -= romanMap.get(year.charAt(i)) | |
} else { | |
y += romanMap.get(year.charAt(i)) | |
} | |
} | |
return y; | |
} | |
console.log(longestDynasty(dynastyReign)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const productProfitArray = [ | |
{ "ProductA": -75 }, | |
{ "ProductB": -70 }, | |
{ "ProductC": 98 }, | |
{ "ProductD": 5 }, | |
{ "ProductE": -88 }, | |
{ "ProductF": 29 }, | |
]; | |
function topProduct (productProfitArray) { | |
const length = productProfitArray.length; | |
if (length < 1) return "No Data"; | |
let values = []; | |
productProfitArray.forEach(e => { | |
values.push(Number(Object.values(e))) | |
}); | |
const v = values.sort((b, a) => { return b - a; })[length - 1]; | |
return productProfitArray.find(el => Number(Object.values(el)) === v); | |
} | |
function bottomProduct (productProfitArray) { | |
const length = productProfitArray.length; | |
if (length < 1) return "No Data"; | |
let values = []; | |
productProfitArray.forEach(e => { | |
values.push(Number(Object.values(e))) | |
}); | |
const v = values.sort((b, a) => { return b - a; })[0] | |
return productProfitArray.find(el => Number(Object.values(el)) === v); | |
} | |
function zeroProfitProduct (productProfitArray) { | |
const length = productProfitArray.length; | |
if (length < 1) return "No Data"; | |
let values = []; | |
productProfitArray.forEach(e => { | |
values.push(Number(Object.values(e))) | |
}); | |
const v = values.reduce((accumulator, currentValue) => | |
accumulator === 0 ? currentValue : | |
currentValue > 0 && currentValue <= Math.abs(accumulator) ? currentValue : | |
currentValue < 0 && -currentValue < Math.abs(accumulator) ? currentValue : accumulator | |
, 0) | |
return productProfitArray.find(el => Number(Object.values(el)) === v) | |
} | |
const topProductValue = topProduct(productProfitArray); | |
const bottomProductValue = bottomProduct(productProfitArray); | |
const zeroProfitProductValue = zeroProfitProduct(productProfitArray); | |
console.log(topProductValue); | |
console.log(bottomProductValue); | |
console.log(zeroProfitProductValue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment