-
Notifications
You must be signed in to change notification settings - Fork 12
Chatbot part4 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
esyasar
wants to merge
3
commits into
hackyourfuturecanada:Elif-Sude-Yasar
Choose a base branch
from
esyasar:chatbot-part4
base: Elif-Sude-Yasar
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Chatbot part4 #25
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| //I declared variable for users inputs | ||
| //and, each input have three possible output. | ||
|
|
||
| var firstData = [ | ||
| { input: ['hello', 'hi', 'greetings'], | ||
| output : ["Hey", "Hello", "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 everyone.'] | ||
| }, | ||
|
|
||
| { input: ['how are you?', 'how is the weather today?', 'how is canada doing in the olympics?'], | ||
| output: ['Fine', 'Great', 'Not so good'] | ||
| }, | ||
|
|
||
| { input: "show me a dog", | ||
| output: showMeaDog() | ||
| }, | ||
|
|
||
| { input: "set an alarm", | ||
| output: delayedAlert() | ||
| } | ||
| ]; | ||
|
|
||
| //console.log(firstData); | ||
|
|
||
| function reply(){ | ||
|
|
||
| // this variable taking the input from what user type and make it lowercase | ||
| var question = document.querySelector("#input").value.toLowerCase(); | ||
|
|
||
| // this variable will store the answers from the chatBot | ||
| var history = document.querySelector("#output"); | ||
|
|
||
| // this is for to choose random number from 0 to 2 | ||
| var randomNumber = Math.floor((Math.random() * 3 )); | ||
|
|
||
| // I assigned an array with no value. | ||
| var filterType = null; | ||
|
|
||
| // this variable searching the input arrays for possible answers. | ||
|
|
||
| const filtered = firstData.filter((item) => item.input.includes(question)); | ||
|
|
||
| let shortAnswer = output[0].output.sort((a, b) => a.length - b.length ); | ||
| let longAnswer = output[0].output.sort((a, b) => b.length - a.length); | ||
|
|
||
| /* I declared a conditional statement to check if users input and data input are matching or not. | ||
| If they are matching, it will give the possible answer depends on which radio button that user clicked. */ | ||
| if(filtered.length > 0) | ||
| { | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix your spacing, no need for so many empty lines |
||
| //if the user choose shortest answer | ||
| if(document.querySelector("#shortest").checked === true) | ||
| { | ||
| var computer = shortAnswer; | ||
|
|
||
| //if the user choose longest answer | ||
| } | ||
| else if(document.querySelector("#longest").checked === true){ | ||
|
|
||
| var computer = longAnswer; | ||
|
|
||
| //if the user choose random answer | ||
| } | ||
| else { | ||
| var computer = filtered.output[randomNumber]; | ||
| } | ||
|
|
||
| // if the user enters a input that is not in the input data | ||
| } | ||
| else { | ||
| history.textContent = "I don't understand that command. Please enter another"; | ||
| } | ||
|
|
||
| history.innerHTML += `You: ${question}\nbot: ${computer}\n` | ||
| }; | ||
|
|
||
| // attached a 'click' event listener to button to call function. | ||
| document.getElementById("submit").addEventListener("click", function(){ | ||
| reply() | ||
| }); | ||
|
|
||
|
|
||
| // function to get image with API and display it on the HTML page | ||
|
|
||
| function showMeaDog(){ | ||
| var image = new XMLHttpRequest(); | ||
| image.onreadystage = function() { | ||
| if (image.readyStage === 4){ | ||
| const data = JSON.parse(image.responseText); | ||
|
|
||
| document.getElementById("apiImage").setAttribute('src', data.message) | ||
| } | ||
| } | ||
| image.open('GET', "https://dog.ceo/api/breeds/image/random", true); | ||
| image.send(null); | ||
| } | ||
|
|
||
| // function to show a message after 2 seconds | ||
| function delayedAlert(){ | ||
| setTimeout( function(){ | ||
| alert("Did you forget about me? It’s your friend, the Alarm!");},2000); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't forget to clean up your code. Don't leave console logs in here