第一个Javascript程序。我究竟做错了什么?

时间:2012-11-20 05:40:11

标签: javascript

我终于开始用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);

5 个答案:

答案 0 :(得分: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.03(数组的长度)。这可以为您提供012

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

学习从零开始处理数组可能会非常棘手。你有点习惯了。最终


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

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

答案 1 :(得分:3)

问题是数组的.length属性计算数组中从零开始的元素数。因此,例如activity包含元素1到5,因此根据Javascript,.length实际上是 6 。然后你的随机数计算将选择1到7之间的数字,超过数组的末尾。这是undefined来自的地方。

您可以通过将索引编号设置为0而不是1来解决此问题,因此activity将包含元素0到4,.length为5.同时从+1中删除{{1}}你的选择计算。

答案 2 :(得分:2)

当您使用“选择器”时,您不希望在{Math.floor函数中包含+1

考虑这个数组:

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

答案 3 :(得分: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 );​

答案 4 :(得分: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>');