#?面試題
##?第一題
###?用js實(shí)現(xiàn)隨機(jī)選取10-100之間的10個(gè)數(shù)字,存入一個(gè)數(shù)組,去重后求和(保證這10個(gè)數(shù)字不能出現(xiàn)重復(fù))
要求:去重不能使用Set
請完善下面的題目
```javascript
//?獲取任意項(xiàng)10-100隨機(jī)數(shù)數(shù)組
const?getRandomNumberList?=?(listLength:?number)?=>?{
??let?list:number[]?=?[];
??for(?var?i?=?0;?i?<?listLength;?i++){
????list.push(getRandomNumber(10,100));
??}
??return?list;
}
//?獲取任意范圍數(shù)
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;
}
// 最終結(jié)果
function?sumOfRandomDistinctTenNumbers(){
??const?list?=?getRandomNumberList(10);
??const?newList?=?setList(list);
??return?getSum(newList);
}
```
##?第二題
給定一個(gè)編碼字符,按編碼規(guī)則進(jìn)行解碼,輸出字符串。編碼規(guī)則是`count[letter]`,將letter的內(nèi)容count次輸出,count是0或正整數(shù),letter是區(qū)分大小寫的純字母,支持嵌套形式。
示例:
```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)?=>?{
??//若不存在?返回當(dāng)前字符串
??if(str.indexOf('[')?===?-1){
??????return?str
??}
??//正則表示?整數(shù)[字符串]?并提取出所有匹配字符串
????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)//次數(shù)
??????let?char?=?item.substring(s+1,e)//字符
??????let?charStr?=''
??????for(let?i?=?0;i?<?Number(num);?i++){
????????charStr?+=?char;
??????}
????????str?=?str.replace(item,charStr)//替換原字符串的匹配內(nèi)容成新字符串
????})
?????return?decodeString(str);//再次重新解讀新字符串
??}
```
##?第三題
基于?React?框架寫一個(gè)列表,列表每項(xiàng)有一個(gè)刪除該項(xiàng)的功能。
請完善下面的題目
```javascript
'use?strict';?//?嚴(yán)格模式
// 父組件
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 {
? // 刪除指定下標(biāo)項(xiàng)
? handleDeleteByIndex: (index: number) => void;
? // 下標(biāo)
? 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}個(gè)</span>
? ? ? ? <button onClick = {()=>{this.handleDelete(index)}}>刪除</button>
? ? ? </div>
? ? )
? }
}
export default Item;
```