javascript – How to capture a string of more than one word and turn it into a single word separated by dashes

I work with a wordpress site. I've created a function with javascript that adds a class to the body tag that I'm going to use to display a different background depending on the category. It works well when the category is a single word. But when I have a category that consists of 2 words, it stops working.
That's the code:

(window.onload = function () {
let cat = document.querySelector (& # 39 ;. cats & # 39;). lastElementChild.textContent;
body = document.querySelector (& # 39; body & # 39;);
body.classList.add (cat.toLowerCase ());
}) ();

If the category is "dentist", this string is added to the body tag, but if it is "General Doctor", it is no longer added.

I use functions.php to call this function only when there is a message using if is_single ().

What can I do to have this "general practitioner" added as a general physician to body tag classes?