mardi 22 mars 2016

When I try to loop my array i get: cannot read property 'length' of undefined -JS-

I'm new in javascript the main goal of this code is to type a question in the textbox the browser will check the question if it was in the switches statements and get the answer than write it on the paragraph's id="lable".

  • The function randomArray(z)-line[8]- return a random array value .

The console says :

Uncaught TypeError: Cannot read property 'length' of undefined randomArray
.........................
The HTML code :
<body>
<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" autocomplete="off">
<input id="send" type="button" onclick="dosome()"  value="Send">
<input id="delete" type="button" onclick="deleteVal()"  value="Delete"></body>

The Javascript:

var greating , userName;

dosome () ;

var firstHello = [[greating+userName+", How can I help you ?" ], ["Hi  "+userName+" how can i help ?"] ,[ greating+", how can i help ?"]];


function randomArray (z) {  
var realArrayLength = z.length + 1 ;
var random_0toLength = Math.round(Math.floor()*realArrayLength);
for (var x =0; x <= realArrayLength ; x++){
if (random_0toLength === x) {
var y = z [x];
}}
return y ;
};


 function getVal() {
write(randomArray (firstHello)); /* <------ trying to write a radom  value from the firstHello array
*/
 var ask = document.getElementById("input").value;
return ask ;}
var ask = getVal();
function write (x){
var lable = document.getElementById("lable").innerHTML = x;
return lable ;
};

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







//............................... you can ignore this function
function dosome () {

var ask = getVal();
var question = ask.split(" ");
var date = new Date().toTimeString().split(" ")[0]; ;
var userName ="Ahmed" ;

 //5// give you different "greatings" according to ur time

 if (date >= "06:00:00" && date <="11:00:00"){
 greating = "Good morning ";
 var alertTime=""
}
else if (date >= "11:00:00" && date <= "15:00:00"){
greating = "Good afternoon ";
var alertTime=""
}
else if (date >= "15:00:00" && date <="22:00:00"){
greating = "Good evening ";
var alertTime=""
}
else {
greating = " You should have some sleep !";
 var alertTime = greating ; 
};
//5//end

//
if (question[0] === "what"){
switch ( question[1]){

case "time":
            switch (question[2]){
                case "is":
                          switch (question[3]){
                          case "it":
                          write("The time is :"+date+alertTime);
                          break;
                          default:
                          };
                break;
            default:    
            } ;
 break;
case "is":
          switch (question[2]){
              case "your" :
                           switch (question[3]){
                           case "name":
                           write("Alex !");
                           break;
                           case "father":
                           write("Ahmed Erabti , he made me !");
                           break;
                           default:
                           };
              break;
              case "my":
              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);
               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:
 }

};

return userName,greating ;
};
function deleteVal () {

var x = document.getElementById("lable").innerHTML = "" ;
return x ;
};

Aucun commentaire:

Enregistrer un commentaire