Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

cordarro gordon
cordarro gordon
621 Points

switch () case

OK so I am trying to create a simple AI. I want to create a list of replies back to user. If they users type back a certain kind of response .

Example

(AI) Hi how are you ?

(Human) Good, how's you're day going ?

my code

<!DOCTYPE html> <html> <head> <title>Title of the document</title> <link href='https://fonts.googleapis.com/css?family=Arimo' rel='stylesheet' type='text/css'> </head> <style> *{ font-family: 'Arimo', sans-serif; margin:0px; padding:0px; }

body{ width: 100%; height:auto; background: lightgrey; } section{ width:50%; height:300px; background:white; padding:20px; margin:auto; position:relative; top:100px; border-radius:50px 50px 50px 50px; }

frame{

width:50%;
background:#ccccff;
border-radius: 10px 10px 10px 0px;
margin-left:20px;
position:relative;
top:30px;

}

h1{ font-family: 'Arimo', sans-serif; font-weight:100; font-size:36px; padding:20px; }

herinPut{

font-family: 'Arimo', sans-serif; font-weight:100; font-size:36px; padding:20px; }

hisinPut{

width:50%;
background:#694dff;
border-radius: 10px 10px 10px 0px;
margin-left:0px;
position:relative;
top:100px;

}

button{ display:block; background:#e6e6ff; padding:20px; border:none; border-radius: 10px 10px 10px 10px; margin:auto; margin-top: 120px; } button{ font-size: 20px; color:#333; font-weight: 300; } </style> <body>

<section> <div id="frame"> <h1 id = "herinPut">Hi how are you?</h1> <h1 id = "hisinPut">type</div> </div> </section> <button type="button" onclick="response()">Click me</button> <script type="text/javascript">

human = document.getElementById("hisinPut").innerHTML = "Hope all is well.";
ardrema = document.getElementById("herinPut");

  function response(){
        human = human;
            switch (human) {
                case human:
                 human.innerHTML = "test";
                break;
                default:
                    console.log("Na");
            }
    }

</script> </body>

</html>

2 Answers

Damien Watson
Damien Watson
27,419 Points

I'm a bit confused by what the question is, but seeing it is titled 'switch case' I'll go with that.

If you want to check the innerHTML of human you would need to do this:

human = document.getElementById("hisinPut");

function response(){
    switch (human.value) {
        case "test":
            console.log("Why are you testing me?");
        break;
        default:
            console.log("I don't understand what you're saying!");
        break;
    }
}
cordarro gordon
cordarro gordon
621 Points

lol Thanks, Ill try to be more clear next time. But on the other hand, why did you choose to write (human.innerHTML) ?

Damien Watson
Damien Watson
27,419 Points

Hey, sorry been offline... yeh, good point. I guess the user content would come through an input, so:

switch (human.value) {

}