【JavaScript】7-25 念数字 (15分)

题目:

输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu字。十个数字对应的拼音如下:

0: ling 1: yi 2: er 3: san 4: si 5: wu 6: liu 7: qi 8: ba 9: jiu

 

 

输入格式:

输入在一行中给出一个整数,如:1234

提示:整数包括负数、零和正数。

输出格式:

在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如 yi er san si

输入样例:

-600

 

 

输出样例:

fu liu ling ling

 

 

JavaScript代码:

const { parse } = require('path')
var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    var n = parseInt(line)
    var ret = read(n)
    console.log(ret)
})

function read(n) {
    var pinyin = ['ling','yi','er','san','si','wu','liu','qi','ba','jiu']
    var arr = []
    var str = n + ''
    var fuhao = new String()
    var s = new String()
    var result = new String()
    var enter = new String()
    var count = 0

    if (n < 0) {
        str = str.substr(1,str.length)
        fuhao = 'fu '
    } else {
        fuhao = ''
    }

    for (let i = 0; i < str.length; i++) {
        arr.push(str[i] - '0')
    }
    for (let j = 0; j < arr.length; j++) {
        var count = count + 1
        
        if (count == arr.length) {
            enter = ''
        } else {
            enter = ' '
        }
        s += pinyin[arr[j]] + enter
        result = fuhao + s
    }

    return result
}

 

posted @ 2021-01-26 11:09  moonskies  阅读(131)  评论(0编辑  收藏  举报