mardi 22 mars 2016

Trying to input and output from HTML to JS from switches statements

I'm new here and new with javascript too ,I'm trying to make a site that can answer many questions which are included in switches statements . I have issues with output the value in the functions "getVal()" and "write()" the input that has the id="send" doesn't work ... The main goal is to type the question on the textbox"input" and press the button"id=send" then the code answer the question on the "id=lable". The hole code :

<body style="text-align:center; background-color:grey; color:white;
">
<img src="Alexs_face.png">
<p style="border:2px black solid; margin:100px 400px 50px 400px">Ask me !</p>
<p id="lable"></p>
<input id="input" type="text" >
<input id="send" type="button" >
<input id="delete" type="button" >
<script>

function getVal() {
var ask = document.getElementById("input").value;
return ask ;
}
var ask = getVal();
function write (x){
var lable = document.getElementById("lable").value = x;
return lable ;
};

//Capitalize the first letter func :
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
//..............

var question = ask.split(" ");
var date = new Date ;
//var userName ;


//...............................

if (question[0] === "what"){

switch ( question[1]){
case "time":
            switch (question[2]){
                case "is":
                          switch (question[3]){
                          case "it":
                          write("Hi , the time is :"+date);
                          break;
                          default:
                          };
                break;
            default:    
            } ;
     break;
     case "is":
          switch (question[2]){
              case "your" :
                           switch (question[3]){
                           case "name":
                           write("Alex !");
                           break;
                           default:
                           };
              break;
              default:

          };
break;

default: write("unknown");

};}
else if (question[0] === "my"){

switch (question[1]){
case "name":
           switch(question[2]){
               case "is":

                userName = capitalize(question[3]);;
                alert("Your name is saved, "+userName);
                function sendUserName(){

                    return userName ;
                }
               break;

              default: 
           };
break;



default:
};
}
 else if (question[0] === "should" || "could" || "may" || "can" )  {


switch (question[1]) {
case "i" :
      switch(question[2]){
      case "sleep":
      write("Sure ! you can sleep if you want to !!");
      break;
      default:
      }
break;

default:
};


}
if (question[0] === "who"){

switch (question[1]){

case "are":
write ("I'm Alex !");
break;
 case "am":
 write ("My leader !");
default:
   }

   }
  else if (question[0] === "i") {
  switch(question[1]){
  case "love":
  switch(question[2]){
  case "you" :
  write ("I love you too , sir")
  break;

 }
 break;
 };
 }



</script>
</body>

Aucun commentaire:

Enregistrer un commentaire