Write a function getEvolutionRateMessage which takes two numbers before & after. The before value is the initial value of the evolution. The after value is the value after its evolution. An evolution can be calculated as follows:
evolution = (after - before) / before * 100
This function will return a string such that there are three possible outputs:
"A positive evolution of X%."
"A negative evolution of Y%."
"No evolution."
Examples:
getEvolutionRateMessage 11.29 45.79
getEvolutionRateMessage 95.12 66.84
getEvolutionRateMessage 0 27.35
getEvolutionRateMessage 41.26 0
getEvolutionRateMessage 1.26 1.26
"A positive evolution of 306%."
"A negative evolution of 30%."
"A positive evolution of 2735%."
"A negative evolution of 4126%."
"No evolution."
This challenge comes from aminnairi here on DEV. Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (26)
I wrote it in Python, kept it as short as possible while maintaining clarity. š
Some JavaScript.
All this talk of evolution makes me want to Go play Pokemon again. Holy crap I haven't played that in a long time!
rate.go
rate_test.go
Since no one proposed a PHP solution, here I am!
getEvolutionRate()unit tests:getEvolutionRateMessage()unit tests:Here is a Javascript arrow function, that uses nested ternary operators. Someone probably already posted this answer, but hey main point is that I took the challenge and solved by myself.
My javascript solution :
Perl solution. I still don't understand how the values are computed for zeroes, but it passes the tests :-)
Haskell:
My knowledge of statistics is a bit rusty (yet I didn't use rust for this challenge), but I think that an increase from 0 to anything is basically an infinite rate and a decrease from anything to 0 is a negative 100% rate, so I disagree with the examples 3 and 4 and have written my solution accordingly.
Haskell, of course
Output