Your solution looks very good but it is recommended to use let instead of var otherwise very good.
@purnimakumarr
Posted
Thanks for the suggestion, Manuel. I'm a beginner in JS so I don't know alot about the best pratices yet. I would definitely learn about the difference between let and var and make the required changes.
@purnimakumarr the difference is the scope in which the variable is declared, var is global and let is for the code block where you declare it