Advertisement
Guest User

Untitled

a guest
Jan 10th, 2015
2,268
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <title>Math Quiz</title>
  4. </head>
  5. <body>
  6.  
  7. <output id="result"></output>
  8. <a href="test.html">Ask Question</a>
  9. <script type='text/javascript'>
  10. // startup code
  11. var numberA = getRandomNumber();
  12. var numberB = getRandomNumber();
  13. var text = 'What is ' + numberA + ' + ' + numberB + '?';
  14. var response;
  15.  
  16. speechSynthesis.cancel();
  17. var objSpeak = new SpeechSynthesisUtterance();
  18.  
  19. // ask the problem
  20. ask(text, function (err, result) {
  21. if (err) {
  22. document.getElementById('result').innerHTML = 'No Answer.';
  23. } else {
  24. var answer = parseInt(result.transcript);
  25. console.log('results:' + numberA + ' - ' + numberB + ' - ' + answer + ' - ' + result.transcript);
  26. if (answer == numberA + numberB) {
  27. response = 'Right! ' + numberA + ' + ' + numberB + ' is ' + answer + '.';
  28. speak(response);
  29. document.getElementById('result').innerHTML = response;
  30. } else {
  31. response = 'Wrong! ' + numberA + ' + ' + numberB + ' is not ' + answer + '.';
  32. speak(response);
  33. document.getElementById('result').innerHTML = response;
  34. }
  35. }
  36. })
  37.  
  38. // get random number between 1 - 10
  39. function getRandomNumber() {
  40. return Math.floor((Math.random() * 10) + 1);
  41. }
  42.  
  43.  
  44. // ask a question and get an answer
  45. function ask(text, callback) {
  46. // ask question
  47. speak(text, function () {
  48. // get answer
  49. var recognition = new webkitSpeechRecognition();
  50. recognition.continuous = false;
  51. recognition.interimResults = false;
  52.  
  53. recognition.onend = function (e) {
  54. if (callback) {
  55. callback('no results');
  56. }
  57. };
  58.  
  59. recognition.onresult = function (e) {
  60. // cancel onend handler
  61. recognition.onend = null;
  62. if (callback) {
  63. callback(null, {
  64. transcript: e.results[0][0].transcript,
  65. confidence: e.results[0][0].confidence
  66. });
  67. }
  68. }
  69. // start listening
  70. recognition.start();
  71. });
  72. }
  73.  
  74.  
  75. // say a message
  76. function speak(text, callback) {
  77. objSpeak.text = text;
  78. objSpeak.lang = 'en-US';
  79.  
  80. objSpeak.onstart = function (e) {
  81. };
  82.  
  83. objSpeak.onend = function (e) {
  84. if (callback) {
  85. callback();
  86. }
  87. };
  88.  
  89. objSpeak.onerror = function (e) {
  90. if (callback) {
  91. callback(e);
  92. }
  93. };
  94. speechSynthesis.speak(objSpeak);
  95. }
  96. </script>
  97. </body>
  98. </html>
Advertisement
RAW Paste Data Copied
Advertisement