我是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