運行一個簡單的搜索和替換腳本,我寫了一條if語句,如果字符串AKA用戶輸入是空的,要警告「沒有字符串」,而是運行其他是函數,然後運行警報提前如果和其他語句都運行在javascript
let str = ''; // Testing if statement
let findWord = prompt('what word do you want to find');
let replaceWith = prompt('replace with that new word');
const searchAndReplace = (string, oldWord, newWord) => {
if (string == '') { // should it be '', or null, or undefined?
alert('no msg')
} else {
return string.split(oldWord.toLowerCase()).join(newWord.toLowerCase());
}
}
let newString = searchAndReplace(str, findWord, replaceWith);
console.log(newString);
對不起,如果這是一個小白的問題,試圖尋找通過這裏已經
作爲一個說明,使用'prompt'以獲取輸入是非常易怒。爲什麼不是兩個輸入欄和一個按鈕? – tadman
「*代替其他運行的是函數*」 - 你怎麼知道? – Bergi
要麼你正在運行這個函數兩次,要麼你不是。通過在兩者中設置斷點來檢查您的調試器。 – tadman