gpt4 book ai didi

JavaScript "ch.match is not a function"

转载 作者:行者123 更新时间:2023-12-03 05:32:39 25 4
gpt4 key购买 nike

所以我有两个功能。我将把它们都发布在这里:

//A
var ltrToNato = function(ch) {
var x = ch
var nato =
('{"A": "Alpha", "B": "Bravo", "C": "Charlie", "D": "Delta", "E": "Echo", "F": "Foxtrot", "G": "Golf", "H": "Hotel", "I": "India", "J": "Juliet", "K": "Kilo",\
"L": "Lima", "M": "Mike", "N": "November", "O": "Oscar", "P": "Papa", "Q": "Quebec", "R": "Romeo", "S": "Sierra", "T": "Tango", "U": "Uniform", "V": "Victor",\
"W": "Whiskey", "X": "X-Ray", "Y": "Yankee", "Z": "Zulu", "0" : "Zero", "1" : "One", "2" : "Two", "3" : "Three", "4" : "Four", "5" : "Five", "6" : "Six",\
"7" : "Seven", "8" : "Eight", "9" : "Niner"}')
var natoLower = nato.toLowerCase()
var natoAlpha = JSON.parse(nato)
var natoAlphaLower = JSON.parse(natoLower)
if (ch >= "A" && ch <= "Z")
return natoAlpha[ch]
else if (ch >= "a" && ch <= "z")
return (natoAlphaLower[ch])
else if (x = x.match(/[0-9]/g))
return natoAlpha[ch]
else
return x
}

//B
var wordToNato1 = function(str) {
var s = ""
for (var i = 0; i <= str.length; i++) {
s + ltrToNato(i)
}
}

对于第一个函数,我想获取任何字母或数字并返回其 Nato 形式。因此,"A" 将是 Alpha"9" 将是 Niner 等等。 ltrToNato 函数就完全没问题了。没问题。

通过第二个函数,我希望能够获取一个字符串并对每个字符执行相同的操作。例如,wordToNato("ABC123") 将是“Alpha Bravo Charlie One Two Three”。这就是出问题的地方。每次我尝试通过调用 wordToNato 在控制台中运行它时,它都会带来错误消息

ch.match is not a function

所以我不知道出了什么问题......请帮忙

最佳答案

我在这里遗漏了什么吗?

您想通过映射的单词将字母转换为字符串。

因此,只需将输入拆分为字符即可。然后将它们映射到您的单词并用一个空格将所有内容连接在一起。

var MAP ={A:"Alpha",B:"Bravo",C:"Charlie",D:"Delta",E:"Echo",F:"Foxtrot",G:"Golf",H:"Hotel",I:"India",J:"Juliet",K:"Kilo",L:"Lima",M:"Mike",N:"November",O:"Oscar",P:"Papa",Q:"Quebec",R:"Romeo",S:"Sierra",T:"Tango",U:"Uniform",V:"Victor",W:"Whiskey",X:"X-Ray",Y:"Yankee",Z:"Zulu",0:"Zero",1:"One",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Niner"};


function strToNato(str){
return str.toUpperCase().split('').map(l => MAP[l] || l).join(' ');
}

console.log(strToNato('ABC123'));

关于JavaScript "ch.match is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com