无法读取未定义的属性长度--javascript [英] Cannot read property length of undefined - javascript

查看:74
本文介绍了无法读取未定义的属性长度--javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript的新手这个代码的主要目标是在文本框中输入一个问题,浏览器会检查问题是否在switch语句中并获得答案,而不是在段落的id =lable上写下。$



- 函数randomArray(z)-line [8] - 返回一个随机数组值。



控制台说:

< blond> 未捕获TypeError:无法读取undefined randomArray的属性'length'


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


HTML代码:

 <   body  >  
< img src = Alexs_face.png >
< p 样式 = border: 2px黑色固体;保证金:100px 400px 50px 400px > 问我!< / p >
< p id = 标签 > < / p >
< 输入 id = 输入 type = text autocomplete = off >
< input id = 发送 type < span class =code-keyword> = button onclick = dosome() value = 发送 >
& lt; 输入 id = delete type = button onclick = deleteVal( ) value < span class =code-keyword> = 删除 > < / body >

Javascript:







<前la ng =Javascript> var greating,userName;

dosome();

var firstHello = [[greating + userName + ,我该如何帮助你?],[ + userName + 我该如何帮助?],[greating + ,我该如何帮助?]];


function randomArray(z){
var realArrayLength = z.length + 1 ;
var random_0toLength = 数学 .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)); / * < ------尝试从firstHello数组写出一个radom值
* /

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

// 大写首字母func:
function capitalize(string){
return string.charAt( 0 )。toUpperCase()+ string.slice( 1 );
}
// ..............







// ...............................你可以忽略这个功能
function dosome(){

var ask = getVal ();
var question = ask.split( );
var date = new 日期()。toTimeString()。split( )[ 0 ]; ;
var userName = Med;

// 5 //根据你的时间给你不同的贪婪

if (date> = 06:00:00&& date< = 11:00 :00){
greating = 早上好;
var alertTime =
}
else if (date> = 11:00:00&& date< = 15:00:00){
greating = 下午好;
var alertTime =
}
else if (date> = 15:00:00&& date< = 22:00:00){
greating = 晚上好;
var alertTime =
}
else {
greating = 你应该睡一觉!;
var alertTime = greating;
};
// 5 //结束

//
if (问题[ 0 ] === what){
switch (问题[ 1 ]){

case time
< span class =code-keyword> switch
(问题[ 2 ]){
case
开关(问题[ 3 ]){
case it
write( 时间是: + date + alertTime);
break ;
默认
};
break ;
默认
};
break ;
case
switch (问题[ 2 ]){
case your
switch (问题[ 3 ]){
case name
write( Alex!);
break ;
case father
写( Medardo Erabti,他让我成功!);
break ;
默认
};
break ;
case my
switch (问题[ 3 ]){
case name
write( Alex!);
break ;


默认
};
break ;
默认

};
break ;

默认:write( 未知);

};}
else if (问题[ 0 ] === my){

switch (问题[ 1 ]){
case name
< span class =code-keyword> switch
(问题[ 2 ]){
case

userName = capitalize(问题[ 3 ]);;
alert( 保存您的姓名, + userName);
break ;

默认
};
break ;



默认
};
}
else if (问题[ 0 ] === || 可以 || 可能 || 可以){


switch (问题[ 1 ]){
case i
switch (问题[ 2 ]){
case sleep
write( 当然!如果你想要,你可以睡觉o !!);
break ;
默认
}
break ;

默认
};

}
如果(问题[ 0 ] === ){

开关(问题[ 1 ]){

case
write( 我是Alex!);
break ;
case am
write( 我的领导者!);
默认
}

};

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

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





我的尝试:



尝试禁用函数'randomArray(z)'中的'z'参数,并将其替换为数组firstHello的名称,其类型为未定义的paragraf中有lable作为id 。


解决方案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,它是是时候看你的代码执行并确保它能达到你的预期。



调试器允许你逐行跟踪执行,检查变量,你会看到有一点它会停止做你期望的事情。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



如果此代码

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



应该按照我的想法行事,它过于复杂,严重错误,甚至没有达到预期的目的。

阅读有关 Math.round Math.floor

建议的文档:停止在代码中执行随机cha,ges并使用调试器来理解代码正在做什么。


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 :

<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 ="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 .


解决方案

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

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.


这篇关于无法读取未定义的属性长度--javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆