2017-03-16 6 views
2

私は、特定の単語のランク付けが必要なスクリプトを作成しようとしています。私は今日までスコアを維持しようとしているが、それは間違って出てくる。私は、コードをテストし、私はABに応答すると、orientaalの結果は結果がorientaalでなければなりません5.等しい= 3とbloemig = 2これは私が作ったコードは(私は非常に経験豊富ではないよ):IF文を使用したスコアの加算

var orientaal = 0; 
var houtig = 0; 
var bloemig = 0; 
var aromatisch = 0; 
var chypre = 0; 
var citrus = 0; 

var q1 = prompt('Welk element spreekt jou het meest aan? Zet de letters van hoog naar laag (Bijv. DBAC). \n A. Vuur \n B. Lucht \n C. Aarde \n D. Water') 

if(q1.charAt(0) == 'A' || 'a') { 
    orientaal = orientaal + 3; 
}else if(q1.charAt(0) == 'B' || 'b') { 
    bloemig = bloemig + 3; 
}else if(q1.charAt(0) == 'C' || 'c') { 
    houtig = houtig + 3; 
}else if(q1.charAt(0) == 'D' || 'd') { 
    citrus = citrus + 3; 
} 

if(q1.charAt(1) == 'A' || 'a') { 
    orientaal = orientaal + 2; 
}else if(q1.charAt(1) == 'B' || 'b') { 
    bloemig = bloemig + 2; 
}else if(q1.charAt(1) == 'C' || 'c') { 
    houtig = houtig + 2; 
}else if(q1.charAt(1) == 'D' || 'd') { 
    citrus = citrus + 2; 
} 

console.log('orientaal = ' + orientaal); 
console.log('houtig = ' + houtig); 
console.log('bloemig = ' + bloemig); 
console.log('aromatisch = ' + aromatisch); 
console.log('chypre = ' + chypre); 
console.log('citrus = ' + citrus); 
+3

'if(q1.charAt(0)== 'A' || 'a')'あなたが思うようにはしません。 –

+4

if文の中でこのように変わるif(q1.charAt(1)== 'A' || q1.charAt(1)== 'a') –

答えて

8

if(q1.charAt(0) == 'A' || 'a')あなたが思う通りのことをしません。後半は常に真であるので具体的には、q1の最初の文字が'A'、または'a'であれば、これは

を言うtruthy

である(すべての文字列が空の文字列を除いてtruthyです)、あなたの」そこにはいつもパスがあります。次のように

代わりに、switchの使用を検討:

switch(q1[0]) { // strings can be accessed as arrays of characters 
    case 'A': 
    case 'a': 
    orientaal += 3; 
    break; 
    case 'B': 
    case 'b': 
    // ....... 
} 
0

あなたがswitchを使用してAa

var orientaal = 0; 
 
var houtig = 0; 
 
var bloemig = 0; 
 
var aromatisch = 0; 
 
var chypre = 0; 
 
var citrus = 0; 
 

 
var q1 = prompt('Welk element spreekt jou het meest aan? Zet de letters van hoog naar laag (Bijv. DBAC). \n A. Vuur \n B. Lucht \n C. Aarde \n D. Water'); 
 

 
switch(q1[0].toLowerCase()){ 
 
    case 'a': 
 
    orientaal+=3; 
 
    break; 
 
    case 'b': 
 
    bloemig+=3; 
 
    break; 
 
    case 'c': 
 
    houtig+=3; 
 
    break; 
 
    case 'd': 
 
    citrus+=3; 
 
    break; 
 
} 
 

 
console.log('orientaal = ' + orientaal); 
 
console.log('houtig = ' + houtig); 
 
console.log('bloemig = ' + bloemig); 
 
console.log('aromatisch = ' + aromatisch); 
 
console.log('chypre = ' + chypre); 
 
console.log('citrus = ' + citrus);

のための二重のチェックを回避するために、小文字に文字を設定することができます
0

ランキングが常に同じ場合、名前一番上の答えは+3、二番目の+2、三番目の+1、最後の一+0を関連付けられたアクテイジに取得すると、いくつかのデータ構造を使用するだけでこの計算全体を単純化できます。 基本的には、各回答をカテゴリに関連付けるだけで、すべてのスコアを一緒に数える必要があります。

大きな利点は、残りのコードを変更せずに質問とカテゴリを追加できることです。それ以外の場合は、追加する質問ごとにif/elseロジック全体をもう一度記述し、カテゴリを追加した場合は既に持っているif/elseをすべて編集する必要があります。

ここに原理を示します。コードはユーザーの入力をよりよく確認して展開する必要があります。そしてobvはあなた自身が持っている質問を使用します。その他

// Array with all used categories 
var categories = [ 
     'orientaal', 
     'houtig', 
     'bloemig', 
     'aromatisch', 
     'chypre', 
     'citrus' 
    ], 
    // Array containing all the questions and the category of each answer 
    questions = [ 
     { 
      'text' : 'Welk element spreekt jou het meest aan? Zet de letters van hoog naar laag (Bijv. DBAC).', 
      'answers' : { 
       'A' : { 
        'val' : 'Vuur', 
        'category' : 'orientaal' 
       }, 
       'B' : { 
        'val' : 'Lucht', 
        'category' : 'bloemig' 
       }, 
       'C' : { 
        'val' : 'Aarde', 
        'category' : 'houtig' 
       }, 
       'D' : { 
        'val' : 'Water', 
        'category' : 'citrus' 
       } 
      } 
     }, { 
      'text' : 'Welk dier spreekt jou het meeste aan?', 
      'answers' : { 
       'A' : { 
        'val' : 'Draak', 
        'category' : 'orientaal' 
       }, 
       'B' : { 
        'val' : 'Arend', 
        'category' : 'aromatisch' 
       }, 
       'C' : { 
        'val' : 'Wolf', 
        'category' : 'houtig' 
       }, 
       'D' : { 
        'val' : 'Slang', 
        'category' : 'chypre' 
       } 
      } 
     }, { 
      'text' : 'Welke locatie spreekt je het meeste aan?', 
      'answers' : {    
       'A' : { 
        'val' : 'Dennenbos', 
        'category' : 'houtig' 
       }, 
       'B' : { 
        'val' : 'Steppe', 
        'category' : 'orientaal' 
       }, 
       'C' : { 
        'val' : 'Oerwoud', 
        'category' : 'citrus' 
       }, 
       'D' : { 
        'val' : 'Heide', 
        'category' : 'bloemig' 
       } 
      } 
     } 
    ], 
    // Array to save the answers of the user. 
    userAnswers = [], 
    // Var to create the results. 
    scoreList; 

// Let's 'fake' an interface that asks the questions. Replace this with your own form and functions that push the answers to the userAnswers array. 
questions.forEach(function(questionObj, index) { 
     // Create the text for the possible answers from our questionObject. 
    var answerText = Object.keys(questionObj.answers).map(function(letter) { 
      // We just return the letter, plus a dot, plus the value corresponding with that letter. 
      return letter + '.: ' + questionObj.answers[ letter ].val; 
     }).join(', '), 
     // Ask the question, using the text inside our questionObj. 
     prompt = window.prompt('Vraag ' + (index + 1) + ': ' + questionObj.text + ' ' + answerText, ''); 
    // Check if a 'valid' answer was entered. This should be expanded to also check that the input consists of some combination of ABCD 
    // if (prompt && prompt.length === 4) userAnswers.push(prompt); 
    // else alert('Gelieve alle 4 de letters in volgorde te zetten'); 
    // But for now we'll just accept any answer for testing purposes. 
    userAnswers.push(prompt); 
}); 
// Now let's check the score of the answers. We can reduce the users' answers into one value object that we can create by reducing our categories into a 'score list': A plain object with the category as the key adn the count as the value. 
scoreList = userAnswers.reduce(function(scoreList, answer, index) { 
    // Since our answers are in the same order as our questions, we can just use the idnex to get the question associated with this answer. 
    var questionObj = questions[ index ], 
     // By splitting the answer, we get an array containing the 4 individual letters. 
     answerLetters = answer.split(''); 
    // For each letter that is part of the answer, add the correct value to our score list. 
    answerLetters.forEach(function(letter, index) { 
     // The answered letter gives us the correct category 
     var category = questionObj.answers[ letter ].category; 
     // We can just use a static array to transform the index into the score: index 0 = score 3, i1 = s2, i2 = s1, i3 = s0 
     scoreList[ category ] += [ 3, 2, 1, 0 ][ index ]; 
    }); 
    return scoreList; 
}, categories.reduce(function(scoreList, category) { 
    scoreList[ category ] = 0; 
    return scoreList; 
}, {})); 
// Report the results 
alert(JSON.stringify(scoreList)); 
関連する問題