2016-12-08 2 views
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 = ""; 
} 
+0

それはreplace'方法 '正規表現を使用して良いでしょう。 – Anson

+1

'charAt [i]':それは間違っています。 'charAt()'と '[]'のどちらかを選んでください。 – Knu

+0

スクリプトに複数の問題があります。あなたは 'for'ループの中に戻ってくるでしょう。 –

答えて

0

コードを簡略化し、コメントに記載されているものを含めて間違いを修正しました。また、文字の置換にはsubstr()関数を使用しました。今働こうとしているようだ。それをチェックアウト:

var result = document.getElementById('result'); 
 
shiftString(); 
 

 
// Take each character in a given String and shift one letter up in the alphabet 
 
function shiftString() { 
 
    var 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 = userString.substr(0, i) + 'a' + userString.substr(i + 1); 
 
     } 
 
     else if (userString.charAt(i) == 'Z') {   
 
     userString = userString.substr(0, i) + 'A' + userString.substr(i + 1); 
 
     } 
 
     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 
 
     var newChar = String.fromCharCode(userString.charCodeAt(i) + 1); 
 
     userString = userString.substr(0, i) + newChar + userString.substr(i + 1); 
 
     } 
 
    } 
 
    result.innerHTML = userString; 
 
    } 
 
}
<input id="string" type="text" value="aAzZ thisIsCool"/> 
 
<div id="result"></div>

関連する問題