我必須創建一個程式,從給定的字串創建一個新字串,如果第一個或最后一個字符是“P”,則洗掉字串的第一個和最后一個字符。如果條件不滿足,則回傳原始字串。我寫的代碼沒有拋出錯誤,但顯然if條件是錯誤的,因為代碼總是只回傳str。有人可以澄清什么問題嗎?
function remove(str) {
if (str.indexOf(0) === "p" && str.indexOf(-1) === "p") {
return str.substring(1, str.length - 1);
} else {
return str;
}
}
console.log(remove("pparallelepipedp"));
uj5u.com熱心網友回復:
從我上面的評論...
“OP 似乎沒有閱讀過 . 的檔案
String.prototype.indexOf。當然,這兩個條件都不會滿足'pparallelepipedp'.indexOf(0) === "p",'pparallelepipedp'.indexOf(-1) === "p"也永遠不會滿足。”
“...... OP 可能會嘗試
charAt,甚至 可能會嘗試at(后者支持-1引數值)。”
function removeWithHelpOfCharAt(str) {
// the OP's code with the help of `charAt`.
if (str.charAt(0) === 'p' && str.charAt(str.length - 1) === 'p') {
return str.substring(1, str.length - 1);
} else {
return str;
}
}
console.log(
"removeWithHelpOfCharAt('pparallelepipedp') ...",
removeWithHelpOfCharAt('pparallelepipedp')
);
console.log(
"removeWithHelpOfCharAt('Pparallelepipedp') ...",
removeWithHelpOfCharAt('Pparallelepipedp')
);
function removeWithHelpOfAt(str) {
// the OP's code with the help of `at` ...
// ... and a slightly changed way of returning the result.
if (str.at(0) === 'p' && str.at(-1) === 'p') {
str = str.substring(1, str.length - 1);
}
return str;
}
console.log(
"removeWithHelpOfAt('pparallelepipedp') ...",
removeWithHelpOfAt('pparallelepipedp')
);
console.log(
"removeWithHelpOfAt('Pparallelepipedp') ...",
removeWithHelpOfAt('Pparallelepipedp')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
uj5u.com熱心網友回復:
我會選擇切片:
function remove(str) {
if (str[0] === "p" && str.slice(-1) === "p") {
return str.slice(1, -1);
}
return str
}
console.log(remove("pparallelepipedp")); // Returns: parallelepiped
console.log(remove("oparallelepipedp")) // Returns: oparallelepipedp
console.log(remove("pparallelepipedo")); // Returns: pparallelepipedo
您還可以考慮進行正則運算式驗證。您可以在這里試驗正則運算式:https ://regex101.com/
在你的情況下,你可以這樣做:
var regExp = /^p(.*?)p$/;
function remove(str) {
return regExp.test(str) ? str.slice(1, -1) : str
}
console.log(remove('pparallelepipedp')); // Returns: parallelepiped
console.log(remove('oparallelepipedp')); // Returns: oparallelepipedp
console.log(remove('pparallelepipedo')); // Returns: pparallelepipedo
uj5u.com熱心網友回復:
函式 indexOf 回傳您作為引數傳遞的專案的索引。
所以 str.indexOf(0) 會嘗試在 str 中找到 0 并回傳它的索引,如果沒有找到它會回傳 -1。
您可以通過在此處使用示例來更多地了解 indexOf https://www.w3schools.com/jsref/jsref_indexof.asp
因此,您需要使用可以訪問這些特定索引上的值的任何其他方法,而不是使用 indexOf。
我撰寫的代碼是為了讓事情變得更容易,這樣我們就可以輕松地執行我們需要的操作
const remove = (str) => {
// This will convert the string into an array
// https://www.w3schools.com/jsref/jsref_split.asp
str = str.split('');
// Checking if value on first index is P
if (str[0] == 'P'){
// This function will remove the value on first index
// https://www.w3schools.com/jsref/jsref_shift.asp
str.shift();
}
// Similarly check for if value on last index is P
if (str[str.length - 1] == 'P') {
// This will remove the value on last index
// https://www.w3schools.com/jsref/jsref_pop.asp
str.pop();
}
// Other wise join the array back and send it as is
// https://www.w3schools.com/jsref/jsref_join.asp
return str.join('');
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/525744.html
