如何检查字符串中某些字符的存在和数量(例如?

发布于 2025-01-25 13:21:20 字数 974 浏览 1 评论 0原文

我正在进行代码战争(基本面),有一个Kata要求我制作一个程序,让您查看'x''o'o'是否是相同(例如'xxxooo'应返回true'xxxxo'应该返回false)。

利用我对编码的最佳知识(这有点),我构建了此代码,但它行不通。

function XO(str) {
  let string = str;
  const o = string.match(/O/g) + string.match(/o/g);
  const x = string.match(/X/g) + string.match(/x/g);
  if (o.length != x.length) {
    return true;
  } else {
    return false;
  }
}

请告诉我我的代码有什么问题。

这部分是在我更新之后。

我更新了代码,但它说null不是对象。我也更新了变量,但我认为这不是原因。

function XO(str) {
  let string = str;
  const largeO = string.match(/O/g);
  const smallO = string.match(/o/g);
  const largeX = string.match(/X/g);
  const smallX = string.match(/x/g);
  const oCombined = largeO.length + smallO.length;
  const xCombined = largeX.length + smallX.length;
  if (oCombined = xCombined) {
    return true;
  } else {
    return false;
  }
}
console.log(XO("OxX"));

I was on code wars (fundamentals) and there was a kata that asked me to make a program that allows you to see if amount of 'x' and 'o' are the same (for example 'xXXooO' should return true and 'xXxxO' should return false).

Using my best knowledge about coding (which is a little) I built this code but it wouldn't work.

function XO(str) {
  let string = str;
  const o = string.match(/O/g) + string.match(/o/g);
  const x = string.match(/X/g) + string.match(/x/g);
  if (o.length != x.length) {
    return true;
  } else {
    return false;
  }
}

Please tell me what is wrong with my code.

This part is after I updated.

I updated my code but it says that null is not an object. I updated the variables too, and I don't think that is the reason why.

function XO(str) {
  let string = str;
  const largeO = string.match(/O/g);
  const smallO = string.match(/o/g);
  const largeX = string.match(/X/g);
  const smallX = string.match(/x/g);
  const oCombined = largeO.length + smallO.length;
  const xCombined = largeX.length + smallX.length;
  if (oCombined = xCombined) {
    return true;
  } else {
    return false;
  }
}
console.log(XO("OxX"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2025-02-01 13:21:20

一个人需要使用判处'i修改器(或flag)进行'x'x'/'x'的情况不敏感匹配。 'o'/'o'

原因是,人们需要处理null失败 match 对于下一个提供的示例代码,可选链接/?。 nofollow noreferrer“> nullish cocerescing操作员/???

function isXAndOWithSameAmount(value) {
  // always assure a string type  
  value = String(value);

  // in case of a `null` value ... ...count is -1.
  const xCount = value.match(/x/gi)?.length ?? -1;

  // in case of a `null` value ... ...count is -2.
  const oCount = value.match(/o/gi)?.length ?? -2;

  // thus the return value for neither an 'x'/`X`
  // match nor an 'o'/`O` match will always be `false`.
  return (xCount === oCount);

  // in order to achieve another (wanted/requested)
  // behavior one needs to change both values after
  // the nullish coalescing operator accordingly.
}

console.log(
  "isXAndOWithSameAmount('xXXooO') ..?",
  isXAndOWithSameAmount('xXXooO')
);
console.log(
  "isXAndOWithSameAmount('xXoXooOx') ..?",
  isXAndOWithSameAmount('xXoXooOx')
);

console.log(
  "isXAndOWithSameAmount('xXxxO') ..?",
  isXAndOWithSameAmount('xXxxO')
);
console.log(
  "isXAndOWithSameAmount('xOoXxxO') ..?",
  isXAndOWithSameAmount('xOoXxxO')
);

console.log(
  "isXAndOWithSameAmount('') ..?",
  isXAndOWithSameAmount('')
);
console.log(
  "isXAndOWithSameAmount('bar') ..?",
  isXAndOWithSameAmount('bar')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

原因之一可以通过施放匹配的结果和可选的链式?。 .mozilla.org/en-us/docs/web/javascript/referent/global_objects/parseint“ rel =” nofollow noreferrer“> parseint 它将返回(integer)号码值GRATER比零或 >值

function isXAndOWithSameAmount(value) {
  // always assure a string type  
  value = String(value);

  // making use of optional chaining and `parseInt`
  // which will result in (integer) number values
  // grater than zero or the `NaN` value.
  const xCount = parseInt(value.match(/x/gi)?.length);
  const oCount = parseInt(value.match(/o/gi)?.length);

  // since a `NaN` value is not equal to itself
  // the return value for neither an 'x'/`X` match
  // nor an 'o'/`O` match will always be `false`.
  return (xCount === oCount);
}

console.log(
  "isXAndOWithSameAmount('xXXooO') ..?",
  isXAndOWithSameAmount('xXXooO')
);
console.log(
  "isXAndOWithSameAmount('xXoXooOx') ..?",
  isXAndOWithSameAmount('xXoXooOx')
);

console.log(
  "isXAndOWithSameAmount('xXxxO') ..?",
  isXAndOWithSameAmount('xXxxO')
);
console.log(
  "isXAndOWithSameAmount('xOoXxxO') ..?",
  isXAndOWithSameAmount('xOoXxxO')
);

console.log(
  "isXAndOWithSameAmount('') ..?",
  isXAndOWithSameAmount('')
);
console.log(
  "isXAndOWithSameAmount('bar') ..?",
  isXAndOWithSameAmount('bar')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

One needs to use an regex' i modifier (or flag) for case insensitive matches of 'x'/'X' and 'o'/'O'.

And of cause one needs to handle the null return value of a failing match method which for the next provided example code is taken care of by Optional chaining / ?. and the Nullish coalescing operator / ??.

function isXAndOWithSameAmount(value) {
  // always assure a string type  
  value = String(value);

  // in case of a `null` value ... ...count is -1.
  const xCount = value.match(/x/gi)?.length ?? -1;

  // in case of a `null` value ... ...count is -2.
  const oCount = value.match(/o/gi)?.length ?? -2;

  // thus the return value for neither an 'x'/`X`
  // match nor an 'o'/`O` match will always be `false`.
  return (xCount === oCount);

  // in order to achieve another (wanted/requested)
  // behavior one needs to change both values after
  // the nullish coalescing operator accordingly.
}

console.log(
  "isXAndOWithSameAmount('xXXooO') ..?",
  isXAndOWithSameAmount('xXXooO')
);
console.log(
  "isXAndOWithSameAmount('xXoXooOx') ..?",
  isXAndOWithSameAmount('xXoXooOx')
);

console.log(
  "isXAndOWithSameAmount('xXxxO') ..?",
  isXAndOWithSameAmount('xXxxO')
);
console.log(
  "isXAndOWithSameAmount('xOoXxxO') ..?",
  isXAndOWithSameAmount('xOoXxxO')
);

console.log(
  "isXAndOWithSameAmount('') ..?",
  isXAndOWithSameAmount('')
);
console.log(
  "isXAndOWithSameAmount('bar') ..?",
  isXAndOWithSameAmount('bar')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

One of cause can achieve the same behavior by casting the result of match and an optionally chained ?.length into an integer value via parseInt which will return either an (integer) number value grater than zero or the NaN value.

function isXAndOWithSameAmount(value) {
  // always assure a string type  
  value = String(value);

  // making use of optional chaining and `parseInt`
  // which will result in (integer) number values
  // grater than zero or the `NaN` value.
  const xCount = parseInt(value.match(/x/gi)?.length);
  const oCount = parseInt(value.match(/o/gi)?.length);

  // since a `NaN` value is not equal to itself
  // the return value for neither an 'x'/`X` match
  // nor an 'o'/`O` match will always be `false`.
  return (xCount === oCount);
}

console.log(
  "isXAndOWithSameAmount('xXXooO') ..?",
  isXAndOWithSameAmount('xXXooO')
);
console.log(
  "isXAndOWithSameAmount('xXoXooOx') ..?",
  isXAndOWithSameAmount('xXoXooOx')
);

console.log(
  "isXAndOWithSameAmount('xXxxO') ..?",
  isXAndOWithSameAmount('xXxxO')
);
console.log(
  "isXAndOWithSameAmount('xOoXxxO') ..?",
  isXAndOWithSameAmount('xOoXxxO')
);

console.log(
  "isXAndOWithSameAmount('') ..?",
  isXAndOWithSameAmount('')
);
console.log(
  "isXAndOWithSameAmount('bar') ..?",
  isXAndOWithSameAmount('bar')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文