Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<!DOCTYPE HTML>
<head>
<title>My First Chatbot</title>
<title>My First ioChatbot</title>
<link rel="stylesheet" type="text/css" href="chatbot_style.css">
<script type="text/javascript" src="script.js" defer></script>
</head>

<body>
<div id="demo">

<h1> My first chatbot! </h1>
<h1> My first ioChatbot! </h1>
<h3> Talk to your bot!</h3>

<div id="user">
Expand All @@ -22,7 +23,8 @@ <h3> Talk to your bot!</h3>
<br />

<h3> Chat history </h3>
<div id="chatBot">
<div id="ioChatbot">
<img id="dog" src="" width="320px" height="220px" alt="" >
<textarea id="output" name="ChatHistory" rows="15"></textarea>
<br />
</div>
Expand Down
88 changes: 88 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always remove your console logs before you push


//for function
function reply() {

let question = document.getElementById("input").value.toLowerCase();

const response = ioChatbot.filter( item => item.input.includes(question))

// 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';
}

} else if( question === "show me a dog" ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to move this logic up so that your program doesn't have to go through all the logic above in this scenario

dogpictures();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always camel case your names



} else if(question==='set an alarm') {
delayedAlert();

} else {
document.getElementById("output").value="I do not understand that comment. Please enter another."
}
}

document.getElementById("submit").addEventListener("click", function() {reply()});



function dogpictures(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelcase for consistency i.e. dogPictures


let image = new XMLHttpRequest();

image.onreadystatechange=function(){
if(image.readyState===4){
let url= JSON.parse(image.responseText).message;

document.getElementById("dog").setAttribute('src', url);
}

}
image.open("GET", "https://dog.ceo/api/breeds/image/random");
image.send();

}



function delayedAlert(){

setTimeout(function(){
alert("Did you forget about me? it's your friend, the Alarm!")
}, 2000);
}