2017-02-22 5 views
0

EDIT:を追加しましたOBJECTif文と範囲をJavaScriptで

私は、変数、関数から返される前に消えているようだ、関数の本体内で宣言された変数との問題を抱えている:

var customerData = { 
    'Joe': { 
    visits: 1 
    }, 
    'Carol': { 
    visits: 2 
    }, 
    'Howard': { 
    visits: 3, 
    }, 
    'Carrie': { 
    visits: 4 
    } 
}; 

function greetCustomer(firstName) { 
    var greeting = ''; 

    for(var key in customerData){ 
    if(key === firstName){ 
     if(customerData[key]['visits'] === 1){ 
     greeting = "Welcome back, " + firstName + "! We're glad you liked us the first time!"; 
     console.log(greeting); // here to illustrate issue 
     } 
     else if(customerData[key]['visits'] > 1){ 
     greeting = "Welcome back, " + firstName + "! So glad to see you again!"; 
     console.log(greeting); 
     } 
    } 
    else{ 
     greeting = "Welcome! Is this your first time?" 
    } 
    } 

    return greeting; 
} 
greetCustomer("Joe"); 
そして出力:

Welcome back, Joe! We're glad you liked us the first time! // here is the correct greeting from the console output 
=> 'Welcome! Is this your first time?' // this is what I got 
Welcome back, Carol! So glad to see you again! // correct output again 
=> 'Welcome! Is this your first time? // base case again. 

が表示されないgreetingべき機能全体のために代入の値にもアクセスできますか?私はを知っています私はブランチから挨拶を返すことができますが、私はここで何を見ているのかは分かりませんが、誰かが説明できることを願っています。ありがとう。

+2

あなたの出力が明確ではありません!もっと説明してください!あなたは何を期待していますか? –

+1

あなたはデータをループしており、すべての反復は何かを 'greeting'変数に書き出します。最後の反復で設定された値だけが返されます。 – JJJ

+1

変数は、関数全体を通して絶対に見えます。あなたのエラーは別の場所にあります – artemisian

答えて

1

コードは、customerDataオブジェクトの使用可能なキーを反復処理します。 firstNameと一致するキーが見つかると、greetingに割り当ててログに記録します。

はその後は、他のキーを反復処理するを続け、次のキーがfirstNameと一致しない場合(それはおそらくないであろう)、greeting"Welcome! Is this your first time?"が割り当てられます。この値は最終的に関数から返されます。 firstNamecustomerDataの最後のキーになった場合、コードは正常に動作します。そうでない場合はgreetingがデフォルト値に再割り当てされます。

基本的な問題は、ターゲット値を見つけて処理した後でコードが処理を継続し、greetings文字列を再割り当てすることです。変数greetingのスコープまたは可視性に問題はありません。これは、機能するローカル変数greetCustomerです。

これを修正する方法の1つは、挨拶を割り当てた直後に戻ることです(前述のとおり)。別の方法は、breakを使用してforループを終了することです。その後、関数の最後にgreetingが返されます。

function greetCustomer(firstName) { 
    var greeting = ''; 

    for(var key in customerData){ 
    if(key === firstName){ 
     if(customerData[key]['visits'] === 1){ 
     greeting = "Welcome back, " + firstName + "! We're glad you liked us the first time!"; 
     console.log(greeting); // here to illustrate issue 
     } 
     else if(customerData[key]['visits'] > 1){ 
     greeting = "Welcome back, " + firstName + "! So glad to see you again!"; 
     console.log(greeting); 
     } 
     break; // customer name found and appropriate greeting set, exit loop 
    } 
    else{ 
     greeting = "Welcome! Is this your first time?" 
    } 
    } 

    return greeting; 
} 
1

成功する場合は、greeting変数に代わずにすぐに挨拶を返します。しかし、firstnamecustomerDataのキーの1つではない場合は、greeting"Welcome! Is this your first time?" &と設定して、 `firstname。

は[TESTED]このようにコードを変更し

function greetCustomer(firstName) { 
var greeting = ''; 

for(var key in customerData){ 
    if(key === firstName){ 
    if(customerData[key]['visits'] === 1){ 
     return("Welcome back, " + firstName + "! We're glad you liked us the first time!"); 
     console.log(greeting); // here to illustrate issue 
    } 
    else if(customerData[key]['visits'] > 1){ 
     return("Welcome back, " + firstName + "! So glad to see you again!"); 
    } 
    } 
    else{ 
    greeting = "Welcome! Is this your first time?"; 
    } 
} 
return greeting; 
} 
console.log(greetCustomer("Joe"));