<h1 id="title">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</h1>
<p id="count"></p>
html, body {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

h1 {
  color: balck;
	font-family: Verdana;
	font-size: 30px;
  padding: 0 40px;
	
	&._2 {
		font-size: 60px;
	}
	
	&._6 {
		font-size: 55px;
	}
	
	&._10 {
		font-size: 50px;
	}
	
	&._14 {
		font-size: 45px;
	}
	
	&._18 {
		font-size: 40px;
	}
}

p {
  color: gray;
  font: 18px/18px 'Verdana'
}
View Compiled
var value = $('#title').text();

var regex = /\s+/gi;
var wordCount = value.trim().split(regex).length;

// Массив допустимых значений количества слов
var allowedCounts = [2, 6, 10, 14, 18];

if (allowedCounts.includes(wordCount)) {
  $('h1').addClass('_' + wordCount);
}

$('#count').html('Слов в заголовке: ' + wordCount);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js