简体   繁体   English

如何检查字符串中某些字符的存在和数量(例如'x'/'X'和'o'/'O')?

[英]How to check existence and amount of certain characters within a string (e.g. 'x'/'X' and 'o'/'O')?

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 ).我参加了代码大战(基础知识),有一个套路要求我制作一个程序,让您查看'x''o'数量是否相同(例如'xXXooO'应该返回true'xXxxO'应该返回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.我更新了我的代码,但它说 null 不是 object。我也更新了变量,但我认为这不是原因。

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"));

One needs to use an regex' i modifier (or flag) for case insensitive matches of 'x' / 'X' and 'o' / 'O' .对于'x' / 'X''o' / 'O'不区分大小写的匹配,需要使用正则表达式i修饰符(或标志)。

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 / ?.并且需要处理失败match方法的null返回值,下一个提供的示例代码由可选链接 / ?. and the Nullish coalescing operator / ??Nullish 合并运算符 / ?? . .

 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 .其中一个原因可以通过将match结果和可选链接的?.length通过parseInt转换为 integer 值来实现相同的行为,这将返回一个大于零的(整数)数值或NaN

 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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 e4x:如何最好地检查属性的存在? - e4x: how to best check existence of an attribute? jQuery:检查窗口是否小于除像素以外的x个单位(例如em,rem)? - jQuery: Check if window is smaller than x units other than pixels (e.g. em, rem)? 如何检查2位小数? 例如2.2.10 - how to check 2 decimals? E.g. 2.2.10 p:inputMask-如何自定义掩码字符,例如强制输入以大写字母表示 - p:inputMask - how to customize the mask characters e.g. force input to capitalize certain letter 数组中什么是空的? 例如 [空 X 5] - What is empty inside an array? e.g. [empty X 5] 如何对 SQL 查询进行排序,但将某些 UTF-8 字符排序为正常等效字符? (例如É被视为E等) - How can I sort an SQL query but have certain UTF-8 characters be ordered as their normal equivalent? (e.g. É be regarded as E etc) 如何使用 JavaScript 验证图像尺寸(表示为“5X4”)? - How can I validate image sizes (expressed as e.g. “5X4”) using JavaScript? 有没有办法检查字符串中一定数量的大写字符? - Is there a way to check for a certain amount of uppercase characters in a string? javascript - 如何每X个字符分解一个字符串? - javascript - how to break up a string every X amount of characters? 这两行是什么意思:o [x] = o [x] || {}; o = o [x]; - What those two lines mean: o[x] = o[x] || {}; o = o[x];
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM