JSFiddle - React, Tailwind, and code Playground

by Bhavik Bamania

JavaScript

function countHi2(str) {
  // Base case
  if (str.length < 2) return 0;

  // Case 1: "xhi" → skip entire "xhi"
  if (str.startsWith("xhi")) {
    return countHi2(str.slice(3));
  }

  // Case 2: "hi" → count it
  if (str.startsWith("hi")) {
    return 1 + countHi2(str.slice(2));
  }

  // Case 3: move forward
  return countHi2(str.slice(1));
}

console.log(countHi2("ahixhi"))