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
70 changes: 70 additions & 0 deletions chatbot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
let bot = [
{
input: ["hello!", "how are you?", "how is the weather today?"],
output: ["Hi" ,"I am good","Fantastic?"]
},
{
input: ["what is your name?", "how old are you?", "What is your major?"],
output: ["Riham", "very old", "astronaut"]
},
{
input: ["where are you from?", "how many language do you speak?", "do you have pets?"],
output: [" From a planet called the earth", "four ", "no"]
},

];



let outputDom = document.getElementById("output");

function reply() {
let question = document.getElementById("input").value.toLowerCase();
console.log(question)
let randomNumber = Math.floor(Math.random()*3);
let filterType = null;

filterType = bot.filter(element =>
element.input.includes(question)
);
console.log(output);



//filterType = bot.filter( item =>
// item.input == question
// );

let asnwer = ""

if(filterType.length > 0){
let asnwers = filterType[0].output;
if(document.getElementById("shortest").checked==true){
asnwer = asnwers.sort()[0];




}else if (document.getElementById("longest").checked==true){
answer = asnwers.sort()[asnwers.length-1];

}else{
answer = asnwers[randomNumber];


}
}


else {
//document.getElementById('output').innerHTML += `bot` + ` : ` + ``+ `I don't understand that command` + `\n` + `User` + ` : ` + `` + question;
answer="I do not understand";
}

outputDom.innerHTML += `user: ${question}\nbot ${answer}\n`
}


document.getElementById("submit").addEventListener('click', reply);


1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<head>
<title>My First Chatbot</title>
<link rel="stylesheet" type="text/css" href="chatbot_style.css">
<script src="chatbot.js" defer></script>
</head>

<body>
Expand Down