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)
Some JavaScript.
Hi there, nice and short solution! I believe the case for the values
0
&27.35
wont give what you expected. But you are in the right path for sure.Fixed!
I wrote it in Python, kept it as short as possible while maintaining clarity. 😎
Neat! I like what they did with this new string interpolation thing in Python.
I Didn't know it was called a polarity. Learning new things every day. Yay!
Since no one proposed a PHP solution, here I am!
getEvolutionRate()
unit tests:getEvolutionRateMessage()
unit tests:I love those guards you are puting in your code. Really makes me want to go back and do PHP with you like the good ol'times!
Let's do a side project together
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
I always love your take at these challenges in Go. I should get started soon. Looking sharp as always!
And yes, Pokemon is just timelessly good. I could play it all my life haha!
Ah shucks, I am humbled by the praise.
You should, Go is a very easy language to pick up and use.
Only part that may require a little more effort to understand is the concurrency related features of it but just pick those up as you need them.
Haskell:
I long awaited this one. Haha!
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.
Thanks for showing us your solution. What's important is what you achieve at the end of the day!
Perl solution. I still don't understand how the values are computed for zeroes, but it passes the tests :-)
Imagine that instead of going from 0 to 27.35, you are going from 0.0000000001 to 27.35. I think you'll have your answer here. 😉
My javascript solution :
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