- "=='
string中如果需要用嵌套引號(hào),可以轉(zhuǎn)義雙引號(hào)也可以交替使用單引號(hào)
想到一個(gè)段子(不無聊的可略過)
問:有沒有什么無意中暴露自己的經(jīng)歷?
答:某日,一妹子問string這單詞什么意思,張口就答字符串。
- 如果想通過數(shù)組索引來改變一個(gè)字符串某位置的值是不行的膀懈,需要重新給該字符串賦值,應(yīng)該是值傳遞和引用傳遞的區(qū)別
//錯(cuò)誤示范
var myStr = "Bob";
myStr[0] = "J";
//正確示范
var myStr = "Bob";
myStr[0] = "J";
(太基礎(chǔ)了谨垃,看的我好無聊启搂,感覺馬上可以寫一個(gè)關(guān)于各種教程網(wǎng)站的測(cè)評(píng)了。刘陶。胳赌。)
- arr.push(element-value); 在數(shù)組后面拼接element
arr.pop(); - .shift(); function to remove the first item from myArray
unshift(element-value); adds the element at the beginning of the array. - It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.
- 注意:把函數(shù)返回值賦給變量和把函數(shù)賦給變量不一樣
- a queue is an abstract Data Structure where items are kept in order。New items can be added at the back of the queue and old items are taken off from the front of the queue
- 也講到了調(diào)用object屬性時(shí)候的兩種方式:一種是.語法匙隔,一種是[]疑苫,區(qū)別在于[]中可以放有空格的字符串,比如obj["hi xy"], 當(dāng)然這個(gè)“hi xy”也可以賦值給一個(gè)變量hi捍掺,然后obj[hi]撼短, 一個(gè)意思
- 接下來,能表示某個(gè)屬性挺勿,也就可以改變屬性值或者增加屬性并賦給屬性一個(gè)值曲横,注意,刪除
delete obj.prop;
- 從switch case表示方法換成object中k-v表示
- check if the property of a given object exists or not.
.hasOwnProperty(propname)
returns true or false. - record collection answer-gist link
對(duì)英文的需求有點(diǎn)費(fèi)解不瓶,但程序很簡(jiǎn)單 - 遍歷obj記得用for in
function lookUpProfile(firstName, prop){
for(var i = 0; i <contacts.length; i++){
if (firstName===contacts[i].firstName){
for(var p in contacts[i]){
if (prop===p){
return contacts[i][p];
}
}
return "No such property";
}
}
return "No such contact";
}
- generate a random whole number between 0 and 9
要whole number用Math.floor()禾嫉,向下取整,down to the closest number.
Math.floor((Math.random()*10))
random(): Return a random number between 0 (inclusive) and 1 (exclusive): - 在min到max中選一個(gè)隨機(jī)整數(shù)湃番,注意上面寫的夭织,左閉右開,所以要+1
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
- 正則/\d+/gi匹配數(shù)字吠撮,g--global \s--space
// This code counts the matches of expression in testString
var andCount = testString.match(expression).length;
- To create objects using constructor functions.
A constructor function is given a capitalized name to make it clear that it is a constructor
this refers to the new object being created by the constructor - private prop
var Car = function() {
// this is a private variable
var speed = 10;
// these are public methods
this.accelerate = function(change) {
speed += change;
};
this.decelerate = function() {
speed -= 5;
};
this.getSpeed = function() {
return speed;
};
};
var Bike = function() {
var gear;
this.getGear = function(){
return this.gear;
};
this.setGear = function(Gear){
this.gear = Gear;
};
// Only change code below this line.
};
- map
var oldArray = [1, 2, 3];
var timesFour = oldArray.map(function(val){
return val * 4;
});
console.log(timesFour); // returns [4, 8, 12]
- reduce, iterate through an array and condense it into one value.
比如求所有元素和
var array = [4,5,6,7,8];
var singleVal = 0;
singleVal = array.reduce(function(previousVal, currentVal) {
return previousVal + currentVal;
}, 0);
- The filter method is used to iterate through an array and filter out elements where a given condition is not true.
var newArray = oldArray.filter(function(val){
return val<6;
});
- sort:
sort can be passed a compare function as a callback. The compare function should return a negative number if a should be before b, a positive number if a should be after b, or 0 if they are equal.
array.sort(function(a, b) {
return a - b;
});
//smallest to biggest
- array.reverse();
- newArray = oldArray.concat(otherArray);
- string.split(' ') 參數(shù)為分隔符
- arr.join(' ')參數(shù)為連接符
over。下一部分居然是算法題讲竿。泥兰。。
words:
Truncate
quotient
Convert Celsius to Fahrenheit 攝氏度--華氏度