当前位置:网站首页>Method of conversion between JS character and ASCII code

Method of conversion between JS character and ASCII code

2020-11-09 14:53:39 Luo Yihao

In the development process , I met the demand for the output of multiple-choice questions .

So you need to output A、B、C、D、E Options such as .

We can define an array of characters to solve .

You can also use between characters ASCII The relationship between codes is easier to solve .

The solution is as follows :

let letter = 'A'
for(let {value} of questionAttrCopys){
    if(type==3){
        $(".result").append(
            `
                <div>${value}</div>
            `
        )
    }else{
        $(".result").append(
            `
                <div>${letter}.${value}</div>
            `
        )
        letter = letter.charCodeAt() + 1
        letter = String.fromCharCode(letter)
    }
}

From the above code ,

Change the letter to ASCII The method of code is :

letter.charCodeAt()

take ASCII The method of converting code into corresponding letter is :

String.fromCharCode(letter)

 

版权声明
本文为[Luo Yihao]所创,转载请带上原文链接,感谢