-
1.題目要求
答案
function get_letter_interval_2(number_a, number_b) {
var r = [];
var letter = ["", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
if (number_a < number_b) {
for (var i = number_a; i <= number_b; i++) {
if (i % 26 === 0) {
r.push(letter[Math.floor(i / 26) - 1].concat(letter[26]));
}
else {
r.push(letter[Math.floor(i / 26)].concat(letter[i % 26]));
}
}
return r;
}
if (number_a > number_b) {
for (var j = number_a; j >= number_b; j--) {
if (j % 26 === 0) {
r.push(letter[Math.floor(j / 26) - 1].concat(letter[26]));
}
else {
r.push(letter[Math.floor(j / 26)].concat(letter[j % 26]));
}
}
return r;
if (number_a = number_b) {
r.push(letter[Math.floor( number_a/ 26)].concat(letter[number_a % 26]));
}
return r;
}
- 2.從數(shù)組中選出不重復(fù)的數(shù)字
var collection = [1, 1, 1, 2, 2, 3, 4];
function choose_no_repeat_number(collection) {
var result = [];
result = collection.filter(function (value,index,array){ // filter()方法:檢測數(shù)值元素肥矢,并返回符合條件所有元素的數(shù)組。
return index == array.indexOf(value); //indexof()方法:搜索數(shù)組中的元素局劲,并返回它所在的位置伟叛”龇看這個位置是否等于元素第一次出現(xiàn)的位置
})
return result;
}
- 3.找出一個數(shù)組中元素出現(xiàn)頻率最高的元素
var arr = [1, -1, 2, 4, 5, 5, 6, 7, 5, 8, 6];
var m = {};
var mnum = 0;
var mmb;
for (var i = 0; i < arr.length; i++) {
var a = arr[i];
m[a] === undefined ? m[a] = 1 : (m[a]++);
if (m[a] > mnum) {
mmb = a;
mnum = m[a];
}
}
document.write("出現(xiàn)頻率最高的元素為:" + mmb); //出現(xiàn)頻率最高的元素為:5
- 4.從數(shù)組中計算出每個數(shù)的個數(shù)
var collection = [1,1,1,1,2,3,1,3,4,2,3,1,3,4,2];
function grouping_count(collection) {
var h = {};
var num = 0;
for (var i = 0; i < collection.length; i++) {
var a = collection[i];
h[a] === undefined ? h[a] = 1 : (h[a]++);
}
return h;
} //{'1':6, '2':3, '3':4, '4':2}
- 5.將二維數(shù)組變?yōu)橐痪S數(shù)組
var collection = [1, [2], [3, 4]];
function double_to_one(collection) {
var arr1 = (collection + '').split(','); //將數(shù)組轉(zhuǎn)字符串后再以逗號分隔轉(zhuǎn)為數(shù)組
var arr2 = arr1.map(function (value, index, arr) { //map() 方法返回一個新數(shù)組藻烤,數(shù)組中的元素為原始數(shù)組元素調(diào)用函數(shù)處理后的值。
return Number(value);
});
return arr2;
}
- 6.將數(shù)字變?yōu)樽址?/strong>
答案:
var number_map_to_word_over_26 = function(collection){
var c=[]
var r=collection.map(function(value,index,collection){
if(value<=26) {
c= String.fromCharCode(value+96);
}
else{
c=String.fromCharCode(97).concat(String.fromCharCode(value%26+96));
}
return c;
})
return r;
};
- 7.將數(shù)組的數(shù)字進(jìn)行排序操作
//按照數(shù)值的大小對數(shù)字進(jìn)行排序爽雄,必須使用一個排序函數(shù):a代表數(shù)組的前一位蝠检,b代表數(shù)組的后一位。
var arr = [1,2,3,5,2,5,3,6,2,6,2,6,2,5,9,6,8,54,3,6,8];
arr.sort(function(a,b){return a-b}); //這樣是升序排列挚瘟。
//如果希望降序排列叹谁,就寫成return b-a;
- 8.求數(shù)組的中位數(shù)
答案
function compute_chain_median(collection) {
//在這里寫入代碼
var ary = collection.split('->').sort(function (a, b) { //split()將chain以指定的"->”為分界乘盖,分割成一個數(shù)組焰檩,結(jié)果是一個數(shù)組
return a - b; //sort() 將數(shù)組進(jìn)行升序操作
});
var lowMiddle = Math.floor((ary.length - 1) / 2); // Math.floor()向下取整
var highMiddle = Math.ceil((ary.length - 1) / 2); //Math.ceil () 向上取整
return (Number(ary[lowMiddle]) + Number(ary[highMiddle])) / 2;
}
- 9.編寫一個函數(shù),實(shí)現(xiàn)以下功能:計算出一個字符串共有多少個單詞組成订框。
<script type="text/javascript">
var r = 0;
var n;
function countWords(message) {
n = message.split(" ") //split() 方法用于把一個字符串分割成字符串?dāng)?shù)組析苫。
r = n.length; //n=Good,morning,,I,love,JavaScript
return r;
}
countWords('Good morning, I love JavaScript.');
document.write(r);
</script> //5
-
10 .題目
答案
var r = [];
for (var i = 0; i < collection.length;) {
let count = 0;
for (var j = 0; j < collection.length; j++) {
if (collection[i] === collection[j]) {
count++;
}
}
r.push({
key: collection[i],
count: count
})
i += count;
}
return r;
}
-
11.題目要求(如何遍歷對象,如何使用正則表達(dá)式)
let result = [];
let H = {};
for (let item of collection) {
if (item.length === 1) {
H[item] === undefined ? H[item] = 1 : H[item] += 1;
} else {
let grexNum = /\d{1,2}/; //正則表達(dá)式 :獲取兩位數(shù)以內(nèi)的數(shù)字
let grexWord = /^\w/; //正則表達(dá)式: 獲取首位字母的字母
let num = item.match(grexNum) //match() 方法可在字符串內(nèi)檢索指定的值穿扳,或找到一個或多個正則表達(dá)式的匹配
let word = item.match(grexWord)
H[word[0]] === undefined ? H[word[0]] = Number(num[0]) : H[word[0]] += Number(num[0]);
}
}
let r = [];
for (let i in H) { //遍歷對象H 賦值給數(shù)組r
r.push({
name: i,
summary: H[i]
})
}
return r;
}
- 11.遍歷對象數(shù)組
var collection_a = [
{key: "a", count: 3},
{key: "e", count: 7},
{key: "h", count: 11},
{key: "t", count: 20},
{key: "f", count: 9},
{key: "c", count: 8},
{key: "g", count: 7},
{key: "b", count: 6},
{key: "d", count: 5}
];
//選出A集合中元素的key屬性跟B對象中value屬性中的元素相同的元素,把他們的count藤违,滿3減1,輸出減過之后的新A集
for (var i = 0; i < collection_a.length; i++) {
for (var j = 0; j < object_b.value.length; j++) {
if (collection_a[i].key === object_b.value[j]) {
collection_a[i].count = collection_a[i].count - Math.floor(collection_a[i].count / 3);
}
}
}
return collection_a;
- 12.for in 和 for of 遍歷的區(qū)別
et iterable = [3, 5, 7];
for (let i in iterable) {
console.log(i); // 0, 1, 2 //for in 是獲得索引值
for (let i of iterable) {
console.log(i); // 3, 5, 7 //for of 是獲得元素值