数组不打印称为方法

时间:2017-02-13 14:03:08

标签: javascript

您好我的代码问题,因为我打印的方法没有显示。该方法不是应该打印的。我想获得用户的语言。我想要它,以便每当它问你说什么语言时它会得到响应但是在一个函数中。相反,我会打印代码而不是语言。

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">
	<div class="form-group">
	    <label>Responce:</label>
            <input type="text" class="form-control" id="input" value="">
	</div>
    </div>
</div>

1 个答案:

答案 0 :(得分: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;
}