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
7 changes: 4 additions & 3 deletions style.css → css/style.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
button {
font-family: Helvetica;
font-size: 10pt;
width: 92px;
/*width: 92px;*/
}

textarea {
Expand All @@ -21,6 +21,7 @@ body {
margin-left: auto;
margin-right: auto;
padding: 20px;

background-color: #F8F8F8;
border: 1px solid #ccc;
box-shadow: 0 0 10px #999;
Expand All @@ -29,7 +30,7 @@ body {
color: black;
}

#demo input {
#demo #input {
padding: 8px;
font-size: 14px;
border: 1px solid #ddd;
Expand All @@ -49,4 +50,4 @@ input:focus {

textarea:focus {
outline: none;
}
}
32 changes: 24 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
<!DOCTYPE HTML>
<head>
<title>My First Chatbot</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="js/script.js" defer></script>
</head>

<body>
<div id="demo">
<h1>My first chatbot!</h1>
<h3>Talk to your bot!</h3>
<input id="input" type="text" placeholder="Say something" />
<button id="submit">Submit</button>
<h3>Chat history</h3>

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

<div id="user">
<form id="chatbot-form">
<input id="input" type="text" placeholder="Say something" />
<button id="submit">Submit</button>

<input type="radio" name="filterType" class="filterType" id="random" value="Random" checked> Random
<input type="radio" name="filterType" class="filterType" id="shortest" value="Shortest Answer"> Shortest Answer
<input type="radio" name="filterType" class="filterType" id="longest" value="Longest Answer"> Longest Answer
</form>
</div>

<br />

<h3> Chat history </h3>
<div id="chatBot">
<textarea id="output" name="ChatHistory" rows="15"></textarea>
</div>
<br />
</div>

</div>
</body>

</html>
</html>
74 changes: 74 additions & 0 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//1.In your JavaScript code, declare a variable and initialize it as an object.
//2.Add two properties to the object: ‘input’ and ‘output’.
// const chatbot = {
// input:['hello', 'how are you?', 'what is your favourite colour?'],
// output:['hi', 'great!', 'i have so many favorites it\'s hard to choose one.']
// };
//console.log(chatbot);
const chatbot = [
{
input: ['hello', 'hi', 'greetings'],
output: ['hello', 'hey', 'greetings']
},
{
input: ['how are you?', 'how is the weather today?', 'how is Canada doing in the Olympics?'],
output: ['fine', 'great', 'not so good']
},
{
input: ['what is your favourite colour?', 'who is your favourite HYF instructor?', 'who is your role model?'],
output: ['i am not sure', 'i have so many favorites it\'s hard to choose one.', 'i like every one']
},

];
//get a random number between 0 and 2

function reply() {

let randomNumber = Math.floor((Math.random() * 3));
//get the input value ,, and convert it to lowercase
let question = document.getElementById('input').value.toLowerCase();
let filterType = null;
//get the array mach the input value
//const output = chatbot.filter(item => item.input === question);
const output = chatbot.filter(item => item.input.includes(question));
//console.log(output.indexOf(question));

//if there is nothing inside the input then do nothing
if(question === ''){ return false; }
appendToOutput(question);
//check if question founded on the chatbot object
if(output.length > 0){
if(document.getElementById('longest').checked){
lOutput = output[0].output.sort((a, b) => b.length - a.length);
appendToOutput(lOutput[0], 1);
}else if(document.getElementById('shortest').checked){
sOutput = output[0].output.sort((a, b) => a.length - b.length );
appendToOutput(sOutput[0], 1);
}else{
appendToOutput(output[0].output[randomNumber], 1);
}

}else{
//if not found appen this msg to the textarea
appendToOutput("I don't understand that command. Please enter another.",1);
}
}

//this function to add a new message to the top of the textarea
function appendToOutput(msg, sender) {
//who send the msg ? bot or user(you)
sender = (sender) ? 'ChatBot':'You';
let newLine = (sender === 'ChatBot')? '\n':'\n\n';
//get the old value ot textarea and add to it the new msg
document.getElementById('output').textContent = sender + ' : ' + msg + newLine + document.getElementById('output').textContent;
}


//when the form submited do this >>> ,, i have already changed the type of button to submit , so each click of the button will submit the form
document.getElementById('chatbot-form').addEventListener('submit', function(e) {
e.preventDefault();
reply();
document.getElementById('chatbot-form').reset();

})