Topalian JavaScript Robot AI 002 by Christopher Topalian
Topalian JavaScript Robot AI 002 by Christopher Topalian
JavaScript
Robot AI
by
Christopher Andrew Topalian
All Rights Reserved
Copyright 2000-2023
Dedicated
to
God the Father
<!-- Dedicated to God the Father -->
<!-- https://github.com/ChristopherTopalian --
>
<!--
https://github.com/ChristopherAndrewTopalia
n -->
<!-- Topalian_JavaScript_Robot_AI.html-->
<html>
<head>
<title> Topalian JavaScript Robot AI </title>
<script src =
'data/randomResponses.js'></script>
<script>
function calculate(input)
{
try
{
return eval(input);
}
catch(error)
{
return null;
}
}
function createRobot()
{
let context = null;
//-//
let chatBox = ce('div');
chatBox.id = 'chatBox';
chatBox.innerHTML = '';
chatBox.className = 'chatBox';
mainDiv.append(chatBox);
//-//
//-//
//-//
//-//
sendButton.onclick = function()
{
// display what the person typed in
chatBox
ge('chatBox').innerHTML += `<span style
= 'color: rgb(255, 255, 255);'>$
{ge('userInput').value}</span><br>`;
if (userQuestion)
{
let responseObj =
responses.find(function(obj)
{
return
obj.keywords.some(function(keyword)
{
// lowercase and remove
punctuation
let cleanInput =
userQuestion.toLowerCase().replace(/[^\w\
s]/g, '');
let cleanKeyword =
keyword.toLowerCase();
if (responseObj)
{
let randomIndex =
Math.floor(Math.random() *
responseObj.responses.length);
ge('chatBox').innerHTML += `<span
style = 'color: aqua;'>$
{responseObj.responses[randomIndex]}</spa
n><br>`;
if
(responseObj.keywords.indexOf('bye') !== -1)
{
context = null;
ge('chatBox').scrollTop =
ge('chatBox').scrollHeight;
}
else
{
context = responseObj.keywords;
ge('chatBox').scrollTop =
ge('chatBox').scrollHeight;
}
}
else
{
let result = calculate(userQuestion);
ge('chatBox').innerHTML +=
`<span style = 'color: aqua;'>$
{randomResponses[randomIndex]}</span><b
r>`;
ge('chatBox').scrollTop =
ge('chatBox').scrollHeight;
}
}
userInput.value = '';
}
};
}
</script>
</head>
</body>
</html>
// Dedicated to God the Father
// https://github.com/ChristopherTopalian
//
https://github.com/ChristopherAndrewTopalia
n
// utility.js
function ge(whichId)
{
let result =
document.getElementById(whichId);
return result;
}
function ce(whichType)
{
let result =
document.createElement(whichType);
return result;
}
function ba(whichElement)
{
let result =
document.body.append(whichElement);
return result;
}
/* Dedicated to God the Father */
/* https://github.com/ChristopherTopalian */
/*
https://github.com/ChristopherAndrewTopalia
n */
/* style001.css */
body
{
background-color: rgb(40, 40, 40);
color: rgb(255, 255, 255);
}
.mainDiv
{
position: fixed;
top : 0px;
left : 0px;
width: 400px;
padding : 5px;
margin: 20px;
border-radius: 8px;
z-index: 10000;
background-color : rgb(0, 0, 0);
color: rgb(255, 255, 255);
}
.userInput
{
width: 340px;
padding-left: 10px;
padding-right: 10px;
padding-top: 4px;
padding-bottom: 4px;
border-radius: 8px;
background-color: rgb(0,0,0);
color: rgb(255, 255, 255);
}
.robotTextStyle
{
color: rgb(255, 135, 255);
}
.sendButton
{
margin-left: 5px;
border-radius: 8px;
background-color: rgb(0, 0, 0);
color: rgb(255, 255, 255);
}
.sendButton:hover
{
border-color: aqua;
}
.sendButton:active
{
color: rgb(0, 255, 255);
}
.chatBox
{
height: 200px;
padding-left: 10px;
padding-right: 10px;
padding-top: 5px;
padding-bottom: 5px;
background-color: black;
overflow-y: scroll;
}
// Dedicated to God the Father
// https://github.com/ChristopherTopalian
//
https://github.com/ChristopherAndrewTopalia
n
// responses.js
responses: [
`Howdy Buddy`,
`Hi Friend`,
`Hi there`,
`Greetings Friend`,
`Good day`,
`Bonjour`
]
},
{
keywords: [
`what is the weather like`,
`what is the forecast`,
`is it going to be sunny`,
],
responses: [
`It's a beautiful day.`,
`The weather is very nice.`,
`It's sunny and warm.`
]
},
{
keywords: [
`how are you`,
`what's up`,
`what are you up to?`
],
responses: [
`I'm doing good, you?`,
`Having fun and you?`,
`I'm good, how about you?`
]
},
{
keywords: [
`excellent`,
`very good`,
`doing good`
],
responses: [
`Nice`,
`That's nice`,
`Very good`,
`Very Excellent`
]
},
{
keywords:
[
`bye`,
`goodbye`,
`take care`
],
responses:
[
`Bye. Talk to you soon.`,
`Goodbye`,
`Have fun`
]
},
{
keywords:
[
`what are your interests`,
`what do you want to do as a career`,
`what is your current job goal`
],
responses:
[
`Computer Science is fun.`,
`Programming is lots of fun.`,
`I like programming a lot.`
]
}
];
// Dedicated to God the Father
// https://github.com/ChristopherTopalian
//
https://github.com/ChristopherAndrewTopalia
n
// randomResponses.js
OR NOR
0111 1000
RC LC
1010 1100
XOR NAND
0110 1110
CNI MNI
Contra-
0100 0010
-diction
0000
For More Tutorials:
CollegeOfScripting.weebly.com
CollegeOfScripting.wordpress.com
Youtube.com/ScriptingCollege
Twitter.com/CollegeOfScript
GitHub.com/ChristopherTopalian
GitHub.com/ChristopherAndrewTopalian
Sites.google.com/view/CollegeOfScripting
Dedicated to God the Father
This book is created by the
College of Scripting Music & Science.
Always remember, that each time you write a script
with a pencil and paper, it becomes imprinted so
deeply in memory that the material and methods are
learned extremely well.
When you Type the scripts, the same is true. The
more you type and write out the scripts by keyboard
or pencil and paper, the more you will learn
programming!
Write and Type every example that you find.
Keep all of your scripts organized.
Every script that you create increases your
programming abilities.
SEEING CODE, is one thing,
but WRITING CODE is another.
Write it, Type it, Speak It, See It, Dream It.
CollegeOfScripting.weebly.com