3

我终于开始用 Javascript 创建我的第一个小练习程序。我知道这并不优雅。我已经让大部分代码工作了,但是当我运行它几次时,我仍然得到一个“未定义”的字符串。我不知道为什么。有人可以向我解释这个未定义的来源吗?

var work = new Array();
work[1] = "product design";
work[2] = "product system design";
work[3] = "product social media post x5";
work[4] = "product Agent Recruitment system design";
work[5] = "product profile system design";
work[6] = "product Agent testing design";
work[7] = "product customer support";
work[8] = "product promotion";
var course = new Array();
course[1] = "javascript";
course[2] = "mandarin";
course[3] = "javascript practical-Code Academy";
course[4] = "javascript practical-learn Street";
course[5] = "mandarin practical-memrise";
course[6] = "new stuff with audiobooks";
var activity = new Array();
activity[1] = "listen to podcasts";
activity[2] = "chat online";
activity[3] = "Exercise";
activity[4] = "take a walk";
activity[5] = "call a friend";
var picker1 = Math.floor(Math.random()*3+1);
var picker2 = Math.floor(Math.random()*work.length+1);
var picker3 = Math.floor(Math.random()*course.length+1);
var picker4 = Math.floor(Math.random()*activity.length+1);
var group_pick = function(){
  if(picker1 === 1){
    return "Time to work on ";
  } else if(picker1 === 2){
    return "Time to learn some ";
  } else if (picker1 === 3){
    return "Lets relax and ";
  } else {
    return "error in group_pick";
  }
};
var item_pick = function() {
  if (picker1 === 1) {
    return work[picker2] ;
  } else if (picker1 === 2) {
    return course [picker3] ;
  } else if (picker1 === 3) {
    return activity[picker4] ;
  } else {
    return "error in item_pick";
  }
};
var task = group_pick() + item_pick();
document.write(task);
4

5 回答 5

5

数组以零索引开始。当您为1索引分配值时,0会为您创建一个没有值 ( undefined) 的索引。

var arr = new Array();
arr[1] = 'hi!';
console.log(arr); // [undefined, "hi!"]
console.log(arr.length) // 2

长度为2,检查一下。您认为您在该数组中有一项,但长度为 2。

通常不自己管理数组索引会更容易。出于多种原因,通常首选数组文字语法。

var arr = [];
arr.push('hi!');
console.log(arr); // ["hi!"]
console.log(arr.length) // 1

或者直接创建包含项目的数组,非常方便。

var arr = [
  "hi",
  "there!"
];
console.log(arr); // ["hi", "there"]
console.log(arr.length) // 2

一旦你正确地制作了数组,你可以简单地得到一个随机项目:

var arr = ['a','b','c'];
var index = Math.floor(Math.random() * arr.length);
console.log(arr[index]); // "a", "b" or possibly "c"

这是有效的,因为var index将由介于之间0.0和最多但不包括1.0时间3(数组的长度)的随机值计算。哪个可以给你一个01或一个2

所以arr这里有3项目,一个在0,一个在1,一个在2

学习从零开始处理数组可能在心理上很棘手。你有点习惯了。最终。


在此处使用这些提示的工作示例:http: //jsfiddle.net/du5Jb/

我改变了数组的声明方式,并+1var pickerX计算中删除了不需要的。

于 2012-11-20T05:48:37.443 回答
3

问题是数组的属性从零开始.length计算数组中元素的数量。例如有元素 1 到 5,所以根据 Javascript实际上是6。然后你的随机数计算将选择一个从 1 到 7 的数字,超过数组的末尾。这就是它的来源。activity.lengthundefined

您可以通过从 0 而不是 1 开始索引编号来解决此问题,因此activity元素 0 到 4,a.length为 5。也+1从您的选择计算中删除。

于 2012-11-20T05:47:08.073 回答
2

当您使用“选择器”时,您不希望拥有+1`Math.floor 函数的内部。

考虑这个数组:

var array = [ "one", "two", "three" ];

array.length; // 3

长度为 3 - 有道理,里面有 3 个项目。
但是数组是从零开始的。

array[0]; // "one"
array[1]; // "two"
array[2]; // "three"
array[3]; // undefined

因此,当您添加它时+ 1,您将:
a) 无法选择数组中的第一个元素 b) 可以选择比数组中的最后一个元素正好高 1 的数字 ( undefined)

于 2012-11-20T05:52:33.410 回答
1

我看到的问题是,当您生成随机变量时,您正在执行 PickerX + 1 ...

所以正确的做法是没有 +1 的 PickerX。另外,您不应该使用 if 命令的主题,请尝试使用 switch case ...

这是固定代码-

var work = new Array()
        work[0] = "product design";
        work[1] = "product system design";
        work[2] = "product social media post x5";
        work[3] = "product Agent Recruitment system design";
        work[4] = "product profile system design";
        work[5] = "product Agent testing design";
        work[6] = "product customer support";
        work[7] = "product promotion";

var course = new Array();
    course[0] = "javascript";
    course[1] = "mandarin";
    course[2] = "javascript practical-Code Academy";
    course[3] = "javascript practical-learn Street";
    course[4] = "mandarin practical-memrise";
    course[5] = "new stuff with audiobooks";

var activity = new Array();
    activity[0] = "listen to podcasts";
    activity[1] = "chat online";
    activity[2] = "Exercise";
    activity[3] = "take a walk";
    activity[4] = "call a friend";



    var picker1 = Math.floor(Math.random() * 3 +1 );
    var picker2 = Math.floor(Math.random() * work.length );
    var picker3 = Math.floor(Math.random() * course.length );
    var picker4 = Math.floor(Math.random() * activity.length );




var group_pick = function(){
    switch(picker1){
        case 1:
            return "Time to work on ";
        case 2:
            return "Time to learn some ";
        case 3:
            return "Lets relax and ";
        default:
            return "error in group_pick";        
    }
};




var item_pick = function() {
    switch(picker1){
        case 1:
            return work[picker2] ;
        case 2:
            return course [picker3] ;
        case 3:
            return activity[picker4] ;
        default:
            return "error in item_pick";    
    }
};


var task = group_pick() + item_pick();
document.write( task );​
于 2012-11-20T05:54:32.040 回答
0

不要那么辛苦。零是你的朋友。我们去打高尔夫球...

var work = [
    "product design", "product system design",
    "product social media post x5",
    "product Agent Recruitment system design",
    "product profile system design",
    "product Agent testing design",
    "product customer support", "product promotion",
], course = [
    "javascript", "mandarin",
    "javascript practical-Code Academy",
    "javascript practical-learn Street",
    "mandarin practical-memrise", "new stuff with audiobooks",
], activity = [
    "listen to podcasts", "chat online", "Exercise",
    "take a walk", "call a friend",
];
function rint(cap) {
    return (Math.random() * cap) | 0;
}
function pick(item) {
    switch (item) {
    case 0: return "Time to work on " +
        work[ rint(work.length) ];
    case 1: return "Time to learn some " + 
        course[ rint(course.length) ];
    case 2: return "Lets relax and " + 
        activity[ rint(activity.length) ];
    default: return "error";
    }
}
document.write(pick(rint(3)) + '<br>');
于 2012-11-20T06:56:09.810 回答