[英]How to check existence and amount of certain characters within a string (e.g. 'x'/'X' and 'o'/'O')?
我參加了代碼大戰(基礎知識),有一個套路要求我制作一個程序,讓您查看'x'
和'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 不是 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"));
對於'x'
/ 'X'
和'o'
/ 'O'
不區分大小寫的匹配,需要使用正則表達式i
修飾符(或標志)。
並且需要處理失敗match
方法的null
返回值,下一個提供的示例代碼由可選鏈接 / ?.
和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; }
其中一個原因可以通過將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.