2017-02-13 1 views
1

Hallo Ich habe ein Problem mit meinem Code, wenn ich die Methode drucken nichts zeigt. Die Methode druckt nicht wie gewünscht. Ich versuche die Sprache des Benutzers zu bekommen. Ich will es so, dass jedes Mal, wenn es die Frage stellt, welche Sprache du sprichst, es die Antwort bekommt, aber in einer Funktion. Stattdessen bekomme ich Code gedruckt und nicht die Sprache.Array nicht gedruckt genannt Methode

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/Ihre Funktionssprache ist falsch definiert be language() –

+0

@VinodLouis Es wird nur die Funktion gedruckt, nicht die Sprache. – user6860260

+0

Sie müssen Funktionssprache aufrufen() https://jsfiddle.net/823agL1y/2/ –

Antwort

1

Sie brauchen nur eine Sprache-Funktion aufzurufen:

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

Und stellen Sie sicher, dass Sie Rückkehr aus language Funktion:

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

Vielen Dank, es hat funktioniert! – user6860260

Verwandte Themen