2017-02-13 2 views
1

こんにちは私は何も表示されないメソッドを印刷しているときに私のコードに問題があります。この方法は、印刷する必要がありません。私はユーザーの言語を取得しようとしています。私はそれがあなたがどの言語を話すのかという質問をするたびに、それが応答を得るが機能になるようにしたい。代わりに私は言語ではなくコードを印刷します。配列はメソッドと呼ばれません。

let questions = [ 
 
    {text:'What is your name?', audio:'music/openmind.ogg', response : input => 'Hello ' + input + '!' }, 
 
    {text:'How old are you?', response : input => 'That means you were born in ' + (2017 - input) + '.'}, 
 
    {text:'Where are you from?', audio:'music/beone.ogg', response: input => 'You are from ' + (input) + '.'}, 
 
    {text: 'Do you eat healthy?', audio: 'music/becoming.ogg', response: input => 'Acording to my data you are eating ' + (input) + ' and that is healthy!'}, 
 
    {text: 'What is your time?', audio: 'music/becoming.ogg', response: input => 'Where I am located' + (new Date().toLocaleTimeString()) + 'that is the day!'}, 
 
    {text: 'What language do you speak', audio: 'music/becoming.ogg', response: input => 'Acording to me you speak: ' + (language) + '!'} 
 
]; 
 

 
let output = $('#output'), 
 
    input = $("#input"), 
 
    curQuestion; 
 

 
function ask() { 
 
    let qi = Math.floor(Math.random() * questions.length); //depending on your needs, a check could be added if it's been asked directly before or only recycle questions when all are asked 
 
    curQuestion = questions[qi]; 
 
    setOutput(curQuestion.text); 
 
    input.val(''); 
 
} 
 

 
ask(); //first call 
 

 
function respond(){ 
 
    let q = curQuestion; 
 
    if(q.audio) 
 
    new Audio(q.audio).play(); 
 
    setOutput(q.response(input.val())); 
 
    setTimeout(ask, 5000); 
 
} 
 

 
function setOutput(txt){ 
 
    output.html($('<h1>').text(txt)); 
 
} 
 

 

 
$(document).keypress(function(e) { 
 
    if (e.which == 13) { 
 
    respond(); 
 
    return false; 
 
    } 
 
}); 
 

 
function language(){ 
 
    var userLang = navigator.language || navigator.userLanguage; 
 
    document.write (userLang); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <div class="well"> 
 
     <div id="output"></div> 
 
    </div> 
 
    <div class="col-md-2 col-md-offset-5"> 
 
\t <div class="form-group"> 
 
\t  <label>Responce:</label> 
 
      <input type="text" class="form-control" id="input" value=""> 
 
\t </div> 
 
    </div> 
 
</div>

+1

ます。https:// jsfiddle。 net/823agL1y /関数の言語が正しく定義されていないbe language() –

+0

@VinodLouis言語ではなく機能を表示するだけです。 – user6860260

+0

function language()を呼び出す必要がありますhttps://jsfiddle.net/823agL1y/2/ –

答えて

1

あなただけの言語関数を呼び出す必要があります。

{ 
    text: 'What language do you speak', 
    audio: 'music/becoming.ogg', 
    response: input => 'Acording to me you speak: ' + language() + '!' 
} 

をそして、あなたはlanguage関数から返すことを確認してください:

function language() { 
    var userLang = navigator.language || navigator.userLanguage; 
    return userLang; 
} 
+0

ありがとうございました! – user6860260

関連する問題