Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

<blond> Uncaught TypeError: Cannot read property 'length' of undefined randomArray

.........................

The HTML code :
HTML
<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:




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 ="Med" ;

 //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("Medardo 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 ;
};


What I have tried:

Tried to disable the 'z' parametr in the function 'randomArray(z)' and replace it with the name of the array "firstHello" , Its type "undefined in the paragraf that has "lable" as an id .


Posted
Updated 22-Mar-16 19:53pm
Comments
Sergey Alexandrovich Kryukov 22-Mar-16 23:55pm    
The object undefined doesn't have length. :-)
It's not about trying. Just use some JavaScript debugger and see what's going on. The problem sounds trivial. The object undefined is assigned to uninitialized variable, but can also be explicitly assigned to. Non-passed function arguments become this object.

I cannot do it for you, not sure that you provided self-containing code. I cannot see in what line did you throw an exception, why not just showing it? Just debug it; it's not as hard as it may look.

—SA

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

If this code
JavaScript
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 ;
};

is supposed to do what I think, it is overly complicated, heavily bugged and not even near what it is intended to do.
Read documentation about Math.round and Math.floor
Advice: stop doing random cha,ges in the code and use the debugger to understand what the code is doing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900