From c198b7d7b2ac0aa707521583dea0a5aaad7cd7b3 Mon Sep 17 00:00:00 2001 From: terratamo Date: Sun, 22 Sep 2019 18:01:40 -0400 Subject: [PATCH 1/2] hm9 --- index.html | 1 + script.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 script.js diff --git a/index.html b/index.html index c516c16..84a639b 100755 --- a/index.html +++ b/index.html @@ -2,6 +2,7 @@ My First Chatbot + diff --git a/script.js b/script.js new file mode 100644 index 0000000..7b9a001 --- /dev/null +++ b/script.js @@ -0,0 +1,65 @@ +const ioChatbot = [ { + input : "hello", + output : ["Hello", "Hi", "Greetings"] + }, + { + input : "What is your favorite color?", + output : ["i am not sure", "There are too many", "i like everyone",] + }, + { + input : "How are you", + output : ["fine", "great", "not so good"] + } + +] + + + +console.log(ioChatbot); + +function reply() { + + let question = document.getElementById("input").value.tolowerCase(); + + let randomNumber = Math.floor (Math.random()*3) + + let be = document.getElementById("output").value + + let filterType = null + + let response = ioChatbot.filter(item => {return item.input === question } ) + + if (response.length>1){ + + if (document.getElementById("longest").checked == true ){ + + be= response[0].output[2]; + + } else if (document.getElementById("shortest").checked == true ){ + + be = response[0].output[0]; + + } else { + + be = response[0].output[randomNumber]; + } + + + }else { + + be = "i do not understand that comment. Please enter another!" + + } + + + + + + +} + +//document.getElementById("submit").addEventListener("click",function() {reply()}); + +document.querySelector("#submit").addEventListener("click", function(){ + reply(); + }); \ No newline at end of file From 16706c1809bf76b0562c48783d8baff1fb45d7d7 Mon Sep 17 00:00:00 2001 From: terratamo Date: Sun, 6 Oct 2019 17:27:25 -0400 Subject: [PATCH 2/2] ioChatbotweek11 --- index.html | 9 ++-- script.js | 119 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 76 insertions(+), 52 deletions(-) diff --git a/index.html b/index.html index 84a639b..400b6c6 100755 --- a/index.html +++ b/index.html @@ -1,14 +1,14 @@ - My First Chatbot + My First ioChatbot - +
-

My first chatbot!

+

My first ioChatbot!

Talk to your bot!

@@ -23,7 +23,8 @@

Talk to your bot!


Chat history

-
+
+
diff --git a/script.js b/script.js index 7b9a001..e094405 100644 --- a/script.js +++ b/script.js @@ -1,65 +1,88 @@ -const ioChatbot = [ { - input : "hello", - output : ["Hello", "Hi", "Greetings"] - }, - { - input : "What is your favorite color?", - output : ["i am not sure", "There are too many", "i like everyone",] - }, - { - input : "How are you", - output : ["fine", "great", "not so good"] - } - -] - - - -console.log(ioChatbot); - +const ioChatbot= [ + { + input: ['hello', 'hi', 'greetings'], + output: ['Hello', 'Hey', 'Greetings'] + }, + { + input: ['what is your favourite colour?', 'who is your favourite HYF instructor?', 'who is your role model?'], + output: ['I am not sure.','There are too many to choose from', 'I like every one'] + }, + { + input: ['how are you?', 'how is the weather today?', 'how is Canada doing in the Olympics?'], + output: ['Fine', 'Great', 'Not so good'] + }, + +]; + + +console.log(ioChatbot) + +//for function function reply() { - let question = document.getElementById("input").value.tolowerCase(); + let question = document.getElementById("input").value.toLowerCase(); + + const response = ioChatbot.filter( item => item.input.includes(question)) - let randomNumber = Math.floor (Math.random()*3) +// for value 0 to 2 + let randomNumber = Math.floor(Math.random()*3); + +// for longest , shortest , random responses + if(response.length>0){ + + if(document.getElementById('shortest').checked === true){ + + document.getElementById("output").value +="you : "+question+ '\n' + "computer : "+ response[0].output.sort((a, b) => a.length - b.length)[0]+ '\n' +'\n'; + + }else if (document.getElementById('longest').checked === true){ + + + document.getElementById("output").value +="you : "+question+ '\n'+"computer : "+response[0].output.sort((a, b) => b.length - a.length)[response.length-1]+ '\n' +'\n'; + + }else { + + + document.getElementById("output").value +="you : "+question+ '\n' +"computer : "+ response[0].output.sort((a, b) => b.length - a.length)[randomNumber]+ '\n' + '\n'; + } - let be = document.getElementById("output").value + } else if( question === "show me a dog" ) { + dogpictures(); - let filterType = null + + } else if(question==='set an alarm') { + delayedAlert(); + + } else { + document.getElementById("output").value="I do not understand that comment. Please enter another." + } +} - let response = ioChatbot.filter(item => {return item.input === question } ) +document.getElementById("submit").addEventListener("click", function() {reply()}); - if (response.length>1){ - if (document.getElementById("longest").checked == true ){ - be= response[0].output[2]; - - } else if (document.getElementById("shortest").checked == true ){ +function dogpictures(){ - be = response[0].output[0]; + let image = new XMLHttpRequest(); - } else { + image.onreadystatechange=function(){ + if(image.readyState===4){ + let url= JSON.parse(image.responseText).message; + + document.getElementById("dog").setAttribute('src', url); + } - be = response[0].output[randomNumber]; } - - - }else { - - be = "i do not understand that comment. Please enter another!" - - } - + image.open("GET", "https://dog.ceo/api/breeds/image/random"); + image.send(); - - - - } -//document.getElementById("submit").addEventListener("click",function() {reply()}); -document.querySelector("#submit").addEventListener("click", function(){ - reply(); - }); \ No newline at end of file + + function delayedAlert(){ + + setTimeout(function(){ + alert("Did you forget about me? it's your friend, the Alarm!") + }, 2000); + }