.replace不是一个功能 - 无法弄清楚

时间:2016-08-20 12:04:25

标签: javascript replace

这是对我的问题的新修改,希望它符合标准并被视为符合条件。

首先,我设法解决了这个问题。我现在将描述情况以及我认为解决问题的解决方案。

我的代码获取一个字符串(一个电话号码)作为输入,重新格式化它,将其解析为float,并返回给定范围内的电话号码位置。

代码由两个函数组成:1。formatCallNumber(callNum)对输入进行文本操作。 2. SortCallNum(callNumInput) - 负责对范围部分进行排序。

问题在于将调用编号范围的值从排序函数(编号2)传递到格式化函数(编号1)。虽然我在排序函数中将这些值解析为字符串,但.replace函数产生了错误。我(思考)工作的解决方案是将值解析为格式化函数中的字符串。

以下两个函数的代码已更新,似乎按预期工作:

功能1 - 格式化功能:

function formatCallNumber(callNum){
  var formatedCallNum = String(callNum);
  formatedCallNum = formatedCallNum.replace(/\D/g,''); // remove all but digits chars from the string (whitespace, dots, etc)
  formatedCallNum = "0." + formatedCallNum; // add "0." to the callNumber string
  formatedCallNum = parseFloat(formatedCallNum); // parse as float - so it could be compared with other decimals
  return (formatedCallNum);
}

功能2 - 排序功能:

function SortCallNum(callNumInput){
// data [test only]
var shelves = {
    "S1" : {"callStart":"100","callEnd": "223.456", "id": 1},
    "S2" : {"callStart":"223.457","callEnd": "334", "id": 2},
    "S3" : {"callStart":"335","callEnd": "535", "id": 3},
    "S4" : {"callStart":"536","callEnd": "638", "id": 4},
    "S5" : {"callStart":"639","callEnd": "847", "id": 5}
    };
var matchId = "";
document.getElementById("somthing").innerHTML += "you typed the number: " + callNumInput; // output of callNumInput (as inserted by user) 
formatedCallNum = formatCallNumber(callNumInput);

// traverse into shelves object : iteration of objects (key = s1-s5)
for (var key in shelves) {
    if (shelves.hasOwnProperty(key)) {
        matchId = shelves[key].id;
        document.getElementById("somthing").innerHTML += "<br>" + (" -- " + "CallEnd is: " + " -- " + shelves[key].callEnd); // display values of object shelves.key.callend
        document.getElementById("somthing").innerHTML += "<br>" + (" -- " + "CallStart is: " + " -- " + shelves[key].callStart); // display values of object shelves.key.callend
        var formatedCallRangeStart =   formatCallNumber(shelves[key].callStart);
        var formatedCallRangeEnd = formatCallNumber(shelves[key].callEnd);
        console.log(formatedCallRangeStart);
        console.log(formatedCallRangeEnd);
        if ((formatedCallNum <= 0) || (formatedCallNum > 1)){alert('call number not in proper range'); break;}      
        if ((formatedCallRangeStart <= formatedCallNum)&&(formatedCallRangeEnd >= formatedCallNum)){break;}
     }
 }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

正如我所看到的,每个标志应该按预期工作。将string传递到SortCallNum而不是number非常重要。

&#13;
&#13;
function SortCallNum(callNumInput){
    // data [test only]
    var shelves = {
        "S1" : {"callStart":100,"callEnd": "223", "id": 1},
        "S2" : {"callStart":224,"callEnd": "334", "id": 2},
        "S3" : {"callStart":335,"callEnd": "535", "id": 3},
        "S4" : {"callStart":536,"callEnd": "638", "id": 4},
        "S5" : {"callStart":639,"callEnd": "847", "id": 5}
    };
    var matchId = "";
    document.getElementById("somthing").innerHTML += "you typed the number: " +     callNumInput; // output of callNumInput (as inserted by user) 
    formatedCallNum = formatCallNumber(callNumInput);

    // traverse into shelves object : iteration of objects (key = s1-s5)
    for (var key in shelves) {
        if (shelves.hasOwnProperty(key)) {
            matchId = shelves[key].id;
            document.getElementById("somthing").innerHTML += "<br>" + (" -- " + "CallEnd is: " + " -- " + shelves[key].callEnd); // display values of object shelves.key.callend
            document.getElementById("somthing").innerHTML += "<br>" + (" -- " + "CallStart is: " + " -- " + shelves[key].callStart); // display values of object shelves.key.callend
            var formatedCallRangeStart = String(shelves[key].callStart);
            formatedCallRangeStart = formatCallNumber(formatedCallRangeStart);
            var formatedCallRangeEnd = String(shelves[key].callEnd);
            formatedCallRangeEnd = formatCallNumber(formatedCallRangeEnd);
            matchId = shelves[key].id;
            if ((formatedCallRangeStart <= formatedCallNum)&&(formatedCallRangeEnd >= formatedCallNum)){
                break;
            }
        }
    }
    alert (matchId);
}

function formatCallNumber(callNum){

    // callNum = prompt('enter a call number: ');
    formattedCallNum = callNum.replace(/\D/g,''); // remove all but digits chars    from the string (whitespace, dots, etc)
    formattedCallNum = "0." + formattedCallNum; // add "0." to the callNumber string
    formattedCallNum = parseFloat(formattedCallNum); // parse as float - so it could be compared with other decimals
    return (formattedCallNum);
}

SortCallNum('1337')
&#13;
<div id="somthing"></div>
&#13;
&#13;
&#13;

所以,这会有效SortCallNum('1337'),而不是SortCallNum(1337) ...

另一个可能的原因是你盲目地信任prompt的返回值。

<button type="button" onclick="var callNumInput = prompt('enter a call number: '); SortCallNum(callNumInput);"> SortCallNum(test)</button>
  

当用户单击“确定”按钮时,将返回在输入字段中输入的文本。如果用户单击“确定”而未输入任何文本,则返回空字符串。 如果用户点击取消按钮,此功能将返回null   https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt#Example

一点消毒应该有所帮助:

if (callNumInput == null) {
  throw new Error('You have to insert a number between 0 and 999.')
}