2017-02-13 46 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

你需要调用函数语言()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