1
私のコードでは、条件文を使用して適切に処理するために、特殊ケース'Z' 'z'
と' '
を文字列でチェックします。最初のケースでは、これらの文字が表示される場合、私はそれらを'A' 'a'
に設定します。 ' '
が表示された場合は、次の繰り返しに進み、空白を保存するために、continue
を使用します。しかし、空白を保存する代わりに、文字列undefined
を取得します。Caesar Cipher JS:なぜ未使用のスペース結果を無視し続けるのですか?
var result = document.getElementById('result');
var finalString = "";
// Take each character in a given String and shift one letter up in the alphabet
function shiftString() {
var userString;
var storeString = Array();
userString = document.getElementById('string').value;
if (typeof userString === 'string') { // JS must interpret user input as a string
for (var i = 0; i < userString.length; i++) { // Iterate through each character in given string
if (userString.charAt(i) == 'z') { // If we reach the end of the alphabet, start over at the beginning
userString.charAt[i] = 'a';
}
else if (userString.charAt(i) == 'Z') {
userString.charAt[i] = 'A';
}
else if (userString.charAt(i) == ' ') { // Keep spaces as they are
continue; // This results in a string such as 'HelloundefinedWorld'
}
else // Shift to the next character over
userString.charAt[i] = (String.fromCharCode(userString.charCodeAt(i) + 1));
}
console.log(typeof userString)
return displayResult(userString);
}
}
// Take the end result of the translated String and post it to the DOM
function displayResult(a) {
for (var i = 0; i < a.length; i++) {
finalString += a.charAt[i];
}
result.innerHTML = finalString;
}
function clearResult() {
result.innerHTML = "";
}
それはreplace'方法 '正規表現を使用して良いでしょう。 – Anson
'charAt [i]':それは間違っています。 'charAt()'と '[]'のどちらかを選んでください。 – Knu
スクリプトに複数の問題があります。あなたは 'for'ループの中に戻ってくるでしょう。 –