Assessment needed please (JavaScript)

Hello everyone. So there’s this little but slightly complex exercise I’m working on. Here’s the question:

Star Rating
Have the function StarRating(str) take the str parameter being passed which will be an average rating between 0.00 and 5.00, and convert this rating into a list of 5 image names to be displayed in a user interface to represent the rating as a list of stars and half stars. Ratings should be rounded to the nearest half. There are 3 image file names available: “full.jpg”, “half.jpg”, “empty.jpg”. The output will be the name of the 5 images (without the extension), from left to right, separated by spaces. For example: if str is “2.36” then this should be displayed by the following image:

So your program should return the string “full full half empty empty”.
Examples
Input: “0.38”
Output: half empty empty empty empty
Input: “4.5”
Output: full full full full half

And here’s my solution:

It works but, I hard coded this stuff to make it work. And I feel like there’s an easier way to go about it? Maybe using loops? I don’t know. Pls help me check it out and let me know how to go about this better. Thanks!

Here’s the link in text:

====https://codepen.io/Tunjidev/pen/BaQPMBq=====(do not include the =====)

Hello @Adetunji

you doing great well done

there many function in the Math and String would help you

check those

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc

also this string function would help

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

my thought that you get the number before the decimal point and repeat the word full then check the decimal part/fraction and check if bigger than 0 and it less or equal 0.5 then add half to the string and if it more than 0.5 make it full

then you check if there any missing string for those 5 words then add empty for that

hope i explain my idea in good way and also that is not better way but it just another way to solve it

and have a nice day :slight_smile:

1 Like