1

Count gaps in string

Function JavaScript 1 revisions 77 a year ago a year ago
function countGaps(str) {
	let gaps = 0;
	const isWhitespace = ch => ' \t\n\r\v'.indexOf(ch) > -1;

	for (let i = 0; i < str.length; i++)
		if (isWhitespace(str[i]) && !isWhitespace(str[i - 1]))
			++gaps;

	return gaps;
}

This function counts gaps in a string. One gap is a one or several whitespace characters next to each other.