我是JavaScript新手,想学习一些基础知识。Javascript代码 - code check
你能否请检查我是否正确地做了以下事情?如果没有,请概述我还没有做的事情。
我只好:
- 计算诞生的用户每月的数,其中通过对月= 1月11日= 0。
- 以字符串输入 获取子是前三个字符
- 转换为大写
- 查找月份缩写字符串
- 鸿沟这三个字母缩写的起始位置
- (这不是找到月份号的唯一方法,但它允许我们练习在字符串中搜索)
我的代码:
var year = prompt('Enter year of birth as a 4 digit integer');
var month = prompt('Enter the name of the month of birth');
// Chop everything after the first 3 characters and make it lowercase
month = month.substr(0,3).toLowerCase();
// Store your array in months, differently named than the month input
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
"nov", "dec"];
// We then use array.indexOf() to locate it in the array
var pos = months.indexOf(month);
if (pos >= 0) {
// valid month, number is pos
}
我会亲自使用子字符串(0,3)而不是substr,并且您被要求做上层,而不是小写 - 没有太大的区别,但是如果功课... – mplungjan 2012-07-26 04:19:06
说明要求转换为大写,但是您要做'toLowerCase' 。 – sachleen 2012-07-26 04:20:23
这里有什么真正的问题吗?你测试过了吗?它工作吗?如果没有,什么不起作用? – sachleen 2012-07-26 04:22:06