I am building a rating component and wish to associate a rating with a message (pulled from an array of messages). The messages array and rating are dynamic, so there is potential for unequal subrange associations.
Example:
ratingRange = 10; //So 1-10 rating potential
messagesArray = ['Bad', 'Okay', 'Good', 'Amazing']; //This array can be of any length but will never be greater than the maxRating
currentRating = 4;
By the above example:
currentRating [1-3] = messages[0] //('Bad')
currentRating [4-6] = messages[1] //('Okay')
currentRating [7-8] = messages[2] //('Good')
currentRating [9-10] = messages[3] //('Amazing')
Rating of 4 would equate to the message 'Okay'.
Assuming that the messagesArray and ratings are relationally sorted already, the Fn would look like:
function getMessageByRating(
ratingRange: number,
messagesArray: string[],
currentRating: number
) {
}
Optimal tests getMessageByRating(ratingRange, messageArray, currentRating):
getMessageByRating(5, ['Bad', 'Okay', 'Good'], 2) //Bad
getMessageByRating(5, ['Bad', 'Okay', 'Good'], 4) //Okay
getMessageByRating(5, ['Bad', 'Okay', 'Good'], 5) //Good
getMessageByRating(10, ['Bad', 'Okay', 'Good'], 4) //Bad
getMessageByRating(10, ['Bad', 'Okay', 'Good'], 5) //Okay
getMessageByRating(10, ['Bad', 'Okay', 'Good'], 7) //Okay
getMessageByRating(10, ['Bad', 'Okay', 'Good'], 8) //Good
getMessageByRating(10, ['Bad', 'Okay', 'Good', 'Amazing'], 3) //Bad
getMessageByRating(10, ['Bad', 'Okay', 'Good', 'Amazing'], 4) //Okay
getMessageByRating(10, ['Bad', 'Okay', 'Good', 'Amazing'], 6) //Okay
getMessageByRating(10, ['Bad', 'Okay', 'Good', 'Amazing'], 7) //Good
getMessageByRating(10, ['Bad', 'Okay', 'Good', 'Amazing'], 8) //Good
getMessageByRating(10, ['Bad', 'Okay', 'Good', 'Amazing'], 9) //Amazing
Answers like Split array into chunks tend to split the chunks by chunk length and do not have a more uniform distribution between them.