【问题标题】:Swapping consecutive/adjacent characters of a string in JavaScript在 JavaScript 中交换字符串的连续/相邻字符
【发布时间】:2021-12-01 03:03:58
【问题描述】:

示例字符串astnbodei,实际字符串必须为santobedi。在这里,我的系统开始从字符串的左侧读取一对两个字符,首先是 LSB,然后是字符对的 MSB。因此,santobedi 被接收为astnbodei。字符串可以是字母和数字的组合,也可以是偶数/奇数长度的字符。

我目前的尝试

var attributes_ = [Name, Code,
    Firmware, Serial_Number, Label
]; //the elements of 'attributes' are the strings
var attributes = [];

for (var i = 0; i < attributes_.length; i++) {
    attributes.push(swap(attributes_[i].replace(/\0/g, '').split('')));
}

function swap(array_attributes) {
    var tmpArr = array_attributes;
    for (var k = 0; k < tmpArr.length; k += 2) {
        do {
            var tmp = tmpArr[k];
            tmpArr[k] = tmpArr[k+1]
            tmpArr[k+1] = tmp;
        } while (tmpArr[k + 2] != null);
    }
    return tmpArr;
}

msg.Name = attributes; //its to check the code

return {msg: msg, metadata: metadata,msgType: msgType}; //its the part of system code

在上面运行代码 sn-p 时,我收到以下错误:

无法编译脚本:javax.script.ScriptException::36:14 预期:但发现(返回 {__if(); ^ in at line number 36 at column number 14

我不确定错误的含义。我的方法正确吗?有直接的方法吗?

【问题讨论】:

    标签: javascript arrays string algorithm swap


    【解决方案1】:

    您是否尝试过成对遍历数组并使用 ES6 语法进行交换?

    你可以在 ES6 中像这样交换变量: [a, b] = [b, a]

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    以下是一种方法。您拥有的代码无效,因为return 不允许在函数之外。

    let string = "astnbodei";
    let myArray = string.split('');
    let outputArray = [];
    
    for (i=0; i<myArray.length; i=i+2) {
        outputArray.push(myArray[i+1]);
      outputArray.push(myArray[i]);
    }
    
    console.log(outputArray.join(''));

    【讨论】:

      【解决方案3】:

      reduce 任务可以很容易地解决字符串的连续成对字符交换...

      function swapCharsPairwise(value) {
        return String(value)
          .split('')
          .reduce((result, antecedent, idx, arr) => {
            if (idx % 2 === 0) {
      
              // access the Adjacent (the Antecedent's next following char).
              adjacent = arr[idx + 1];
      
              // aggregate result while swapping `antecedent` and `adjacent`.
              result.push(adjacent, antecedent);
            }
            return result;
          }, []).join('');
      }
      console.log(
        'swapCharsPairwise("astnbodei") ...',
        swapCharsPairwise("astnbodei")
      );

      【讨论】:

        【解决方案4】:

        问题中错误的原因是swap函数的放置。但是,切换它的位置给了我另一个错误:

        java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: javax.script.ScriptException: delight.nashornsandbox.exceptions.ScriptCPUAbuseException: Script used more than the allowed [8000 ms] of CPU time.

        @dikuw 的回答部分帮助了我。以下代码行对我有用:

        var attributes_ = [Name, Code,
            Firmware, Serial_Number, Label
        ]; //the elements of 'attributes' are the strings
        var attributes = [];
        
        for (var i = 0; i < attributes_.length; i++) {
            attributes.push(swap(attributes_[i].replace(/\0/g, '').split('')));
        }
        
        return {msg: msg, metadata: metadata,msgType: msgType};
        
        function swap(array_attributes) {
            var tmpArr = [];
            for (var k = 0; k < array_attributes.length; k= k+2) {
                if( (array_attributes[k + 1] != null)) {
                    tmpArr.push(array_attributes[k+1]);
                    tmpArr.push(array_attributes[k]);
            }}
            return tmpArr.join('');
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-02
          • 2012-02-29
          • 1970-01-01
          • 2021-03-13
          相关资源
          最近更新 更多