我參加了代碼大戰(基礎知識),有一個 kata 要求我制作一個程式,讓您查看數量'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 不是一個物件。我也更新了變數,我認為這不是原因。
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"));
uj5u.com熱心網友回復:
對于/和/i的不區分大小寫的匹配,需要使用正則運算式修飾符(或標志) 。'x''X''o''O'
當然,需要處理null失敗方法的回傳值,match對于下一個提供的示例代碼,該方法由可選鏈接 /?.和空值合并運算子 / 處理??。
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該整數值將回傳大于零的(整數)數值或NaNvalue。
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; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/469176.html
標籤:javascript 正则表达式 空值 匹配 类型错误
