# 面试题
## 第一题
### 用js实现随机选取10-100之间的10个数字,存入一个数组,去重后求和(保证这10个数字不能出现重复)
要求:去重不能使用Set
请完善下面的题目
```javascript
// 获取任意项10-100随机数数组
const getRandomNumberList = (listLength: number) => {
let list:number[] = [];
for( var i = 0; i < listLength; i++){
list.push(getRandomNumber(10,100));
}
return list;
}
// 获取任意范围数
const getRandomNumber = (min: number,max: number) => {
return Math.round (Math.random() * (max-min) + min);
}
// 去重
const setList = (list: any[]) => {
let newArr = [];
for( let i = 0 ; i < list.length; i++){
if( newArr.indexOf(list[i]) === -1)newArr.push(list[i]);
}
return newArr;
}
// 求和
const getSum = (list: number[]) => {
let sumNumber:number = 0;
for( let i = 0; i < list.lenght; i++){
sumNumber += list[i]
}
return sumNumber;
}
// 最终结果
function sumOfRandomDistinctTenNumbers(){
const list = getRandomNumberList(10);
const newList = setList(list);
return getSum(newList);
}
```
## 第二题
给定一个编码字符,按编码规则进行解码,输出字符串。编码规则是`count[letter]`,将letter的内容count次输出,count是0或正整数,letter是区分大小写的纯字母,支持嵌套形式。
示例:
```javascript
const s1 = '10[a]2[bc]'; decodeString(s); // 返回'aaaaaaaaaabcbc'
const s2 = '2[3[a]2[bc]]'; decodeString(s); // 返回 'aaabcbcaaabcbc'
```
请完善下面的题目
```javascript
//解读字符串
const decodeString = (str: string) => {
//若不存在 返回当前字符串
if(str.indexOf('[') === -1){
return str
}
//正则表示 整数[字符串] 并提取出所有匹配字符串
let list=str.match(/(\d+)(\[([a-z]|[A-Z])+\])/ig)
list.map((item)=>{
//l为所有匹配字符串
let s = item.indexOf('[')
let e = item.indexOf(']')
let num = item.substring(0,s)//次数
let char = item.substring(s+1,e)//字符
let charStr =''
for(let i = 0;i < Number(num); i++){
charStr += char;
}
str = str.replace(item,charStr)//替换原字符串的匹配内容成新字符串
})
return decodeString(str);//再次重新解读新字符串
}
```
## 第三题
基于 React 框架写一个列表,列表每项有一个删除该项的功能。
请完善下面的题目
```javascript
'use strict'; // 严格模式
// 父组件
import * as React from 'react';
import Item from './Item';
class State {
public list: number[] = new Array(10).fill('');
}
export default class List extends React.Component {
public state = new State();
public handleDeleteByIndex = (index: number) => {
const { list } = this.state;
list.splice(index, 1);
this.setState({
list
});
};
public render() {
const { list } = this.state;
return (
<div>
<h1>列表</h1>
{list.map((v, index) => (
<Item index={index} handleDeleteByIndex={this.handleDeleteByIndex} />
))}
</div>
);
}
}
export default List;
// 子组件
import * as React from 'react';
// Item接口
interface IProps {
// 删除指定下标项
handleDeleteByIndex: (index: number) => void;
// 下标
index: number;
}
class Item extends React.Component <Props> {
constructor(props:any){
super(props);
}
public handleDelete = (index: number) =>{
this.props.handleDeleteByIndex(index);
}
public render(){
const { index } = this.props;
return (
<div>
<span style={{fontSize: 16}}>我是第{index}个</span>
<button onClick = {()=>{this.handleDelete(index)}}>删除</button>
</div>
)
}
}
export default Item;
```