You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
这是一道大题目,把考点拆成了4个小项;需要侯选人用递归算法实现(限制15行代码以内实现;限制时间10分钟内完成):
a) 生成一个长度为5的空数组arr。
b) 生成一个(2-32)之间的随机整数rand。
c) 把随机数rand插入到数组arr内,如果数组arr内已存在与rand相同的数字,则重新生成随机数rand并插入到arr内[需要使用递归实现,不能使用for/while等循环]
d) 最终输出一个长度为5,且内容不重复的数组arr。
// 封装一个去除字符串的函数
/**
* 去除字符串空格
* @param str {String} 需要处理的字符串
* @param type {String} 去除字符串哪部分的空格
* @return {String} 处理后的字符串
**/
function removeSpacing(str, type='all') {
let regList = {
all: /\s+/g,
bothSides: /^\s*|\s*$/g,
left: /^\s*/,
right: /\s*$/,
}
if(!str) return '请传入字符串!';
if(type === 'middle') {
let leftSpace = str.match(/^\s*/g)[0];
let rightSpace = str.match(/\s*$/g)[0];
return leftSpace + str.replace(regList['all'], '') + rightSpace;
}else {
return str.replace(regList[type], '');
}
}
let str3 = ' Hello World! ';
let result = removeSpacing(str3, 'middle');
console.log(result);
3、去除字符串中指定的字符(最后一个)
这里提供三种方案,使用正则的那个方案最优
// 去除指定的字符串中的字符(最后一个)
let str = 'we are the world!We are the children!';
function removeLastCharacter(char, str) {
let index = 0;
let tempArray = str.split('');
// for(let i = 0; i < tempArray.length; i++) {
// if(tempArray[i] === 'e') {
// index = i;
// }
// }
// 查找最后一个字符可以用自带的API方法:lastIndexOf
index = tempArray.lastIndexOf(char);
tempArray.splice(index, 1);
return tempArray.join('');
}
console.log(removeLastCharacter('e', str));
// 第二种比较高级的,使用正则表达式,但是没看懂-_-
let str2 = 'we are the world!We are the children!';
function delLast(char, str) {
let reg =new RegExp(`${char}(?=([^${char}]*)$)`)
return str.replace(reg,'')
}
console.log(delLast('e', str2));
// 还有一种暴力的方法
let str3 = 'we are the world!We are the children!';
function delLast_1(char, str) {
return str.split('').reverse().join('').replace(char, '').split('').reverse().join('');
}
console.log(delLast_1('e', str3));
4、把下划线命名改为大驼峰命名
这里还是直接放代码吧:
// 把下划线命名改成大驼峰命名
let varName = 'a_sdsd_ds_d_s_g_h';
function underlineToCamel(str) {
if(typeof str !== 'string' || str === '') {
return '请传入正确的字符串!'
}
let tempArray = str.split('');
let newArray = [];
let index = 0;
for(let i = 0; i < tempArray.length; i++) {
if(i == 0) {
newArray.push(tempArray[i].toUpperCase());
}
if(tempArray[i] === '_') {
newArray.push(tempArray[i + 1].toUpperCase());
i = i + 1; // 跳过下划线后面的那个小写,不然就是这种 ["A", "a", "S", "s", "d", "s", "d", "D", "d", "s", "D", "d"]
continue;
}else {
newArray.push(tempArray[i]);
}
}
return newArray.join('');
}
let result = underlineToCamel(varName);
console.log(result);
// 第二种方法
let varName1 = 'y_must_go';
function toBigCamel(str) {
if(typeof str !== 'string' || str === '') {
return '请传入正确的字符串!'
}
let tempArray = str.split('_');
for(let i = 0; i < tempArray.length; i++) {
tempArray[i] = tempArray[i].replace(/^\w/, tempArray[i][0].toUpperCase()); // 字符串当数组用-_-
}
return tempArray.join('');
}
let result1 = toBigCamel(varName1);
console.log(result1);
// 第三种
function strToCamel(str){
return str.replace(/(^|_)(\w)/g,(m,$1,$2)=>$2.toUpperCase());
}
console.log(strToCamel('a_c_def'));
console.log(strToCamel('y_must_go'));
1、 用递归算法实现,数组长度为5且元素的随机数在2-32间不重复的值
这是一道大题目,把考点拆成了4个小项;需要侯选人用递归算法实现(限制15行代码以内实现;限制时间10分钟内完成):
a) 生成一个长度为5的空数组arr。
b) 生成一个(2-32)之间的随机整数rand。
c) 把随机数rand插入到数组arr内,如果数组arr内已存在与rand相同的数字,则重新生成随机数rand并插入到arr内[需要使用递归实现,不能使用for/while等循环]
d) 最终输出一个长度为5,且内容不重复的数组arr。
另外一种实现方式:
这里补充一点关于JS生成随机数的知识
首先要了解几个函数
1、生成[min, max]之间的整数。
2、生成[min, max)之间的整数。
3、生成(min, max]之间的整数。
3、生成(min, max)之间的整数。
2、去掉字符串里面的空格
这个题目有嘿多变种,常见的就是去除字符串两边的空格,实际上JS里面已经封装过一个叫trim的方法,考虑兼容性我们这样做就可以了。
如果是直接去除所有的空格,方法比较多也很简单,其中可以使用split和join这两个api来实现:
当然我们还可以自定义一个函数来实现去掉不同位置的空格
3、去除字符串中指定的字符(最后一个)
这里提供三种方案,使用正则的那个方案最优
4、把下划线命名改为大驼峰命名
这里还是直接放代码吧:
第三种方法里面运用到了一些知识点,请参考这篇文章的3.4节
The text was updated successfully, but these errors were encountered: