Comments

Tuong Vi Pham2 years ago

const solve = (strArg) => {
  const strArray = strArg
    .split(/[^a-zA-Z]+/g)
    .filter(Boolean);
  return strArray.reduce((acc, cur) => {
    acc += cur === strArray[0] 
      ? cur.toLowerCase()
      : cur.charAt(0).toUpperCase() + cur.slice(1);
    return acc;
  }, '');
};

Bryan Guilas2 years ago

const solve = (strArg) => {
	let str = strArg
		.replace(/[^a-zA-Z]/g, ' ')
		.toLowerCase()
		.split(' ');
	let res = str
		.map(word => word.slice(0, 1).toUpperCase() + word.slice(1))
		.join('');
	
	return res.slice(0, 1).toLowerCase() + res.slice(1);
};

solutions Bedfordshire2 years ago

A one-liner, not great though...

const solve = (strArg) => strArg.split("").map((x, index) => strArg[index - 1] < 'A' || strArg[index - 1] > 'z' ? x.toUpperCase() : x.toLowerCase()).filter(x => x >= 'A' && x <= 'z' ).map((x, index) => index === 0 ? x.toLowerCase() : x).join("");

Ameliorated Tugrik2 years ago

a piece of cake! :D const solve = str => str.match(/\b[a-zA-Z]+\b/g).map((i, n) => (n === 0 ? i[0].toLowerCase() : i[0].toUpperCase()) + i.slice(1)).join('')

Forges2 years ago

const solve = strArg => {
  const found = strArg.match(/[a-zA-Z]+/g);

  return found
    .map((item, index) => (index === 0 ? item.toLowerCase() : item[0].toUpperCase() + item.slice(1)))
    .join('');
};

Coordinator proactive2 years ago

Basically the same, but with a dumber (pointlessly intricate) regex, and a reduce() integrating map() + join() into one iteration. I|m also lowercasing all letters in n1+ words in case they contain some uppercase characters inside:

const solve = (strArg) => {
  return strArg
    .match(/[^0-9|\W]\w+/g)
    .reduce((a, w, i) => {
      if (i === 0) {
        return a + w.toLowerCase();
      } else {
        return a + (w[0].toUpperCase() + w.slice(1).toLowerCase());
      }
    }, '');
};

zunair jarral3 years ago

is there anybody advise on One Liner code

const solve = (strArg) => { let a = strArg.replace(/[^a-z\s]/gi, " ").toLowerCase().trim().split(" "); let b = a.slice(0,1); let c = a.slice(1); let d = c.map((pro) => { let firstLetter = pro.slice(0,1); let capitalized = firstLetter.toUpperCase(); return pro.replace(firstLetter, capitalized) }); let e = d.filter(n => n); return [...b, ...e].join(""); };

Abdelhamid Ismail3 years ago

Good job mate!

this regular expressions drive me nuts! :D

your soulution in better formating

const solve = (strArg) => {
  let a = strArg
    .replace(/[^a-z\s]/gi, " ")
    .toLowerCase()
    .trim()
    .split(" ");

  return [
    a[0],
    ...a
      .slice(1)
      .filter((n) => n)
      .map((e) => e[0].toUpperCase() + e.slice(1)),
  ].join("");
};