freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../intermediate-algorithm-scri.../binary-agents/index.md

8.2 KiB
Raw Blame History

title localeTitle
Binary Agents 二元代理商

:triangular_flag_on_post:如果卡住,请记得使用**Read-Search-Ask** 。尝试配对程序:busts_in_silhouette:并编写自己的代码:pencil:

问题说明:

这个问题非常简单,您将获得一个代表二进制代码中的句子的字符串,您需要将其翻译成单词。没有直接的方法,所以你必须翻译两次。

相关链接

:speech_balloon:提示1

在将这些值转换为字符之前,您应该先将二进制转换为十进制

现在尝试解决问题

:speech_balloon:提示2

当关注较小的部分时,事情会变得更容易,将输入分成当前一个字母的焦点。

现在尝试解决问题

:speech_balloon:提示3

确保每次将字符从二进制转码为十进制时,都会重置用于跟踪那些字符的变量。另外不要忘记把所有东西都变回一个字符串。

现在尝试解决问题

扰流警报!

警告牌

提前解决!

:beginner:基本代码解决方案

    function binaryAgent(str) { 
      biString = str.split(' '); 
      uniString = []; 
 
    /*using the radix (or base) parameter in parseInt, we can convert the binary 
      number to a decimal number while simultaneously converting to a char*/ 
 
      for(i=0;i < biString.length;i++){ 
        uniString.push(String.fromCharCode(parseInt(biString[i], 2))); 
      } 
 
      // we then simply join the string 
      return uniString.join(''); 
    } 
 
    // test here 
    binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); 

:rocket: 运行代码

代码说明:

  • 将字符串分隔为由空格分隔的字符串数组。
  • 创建一些在整个过程中需要的变量,这些名称在很大程度上是自我解释的。
  • 遍历新数组中的每个二进制字符串。
  • 使用parseInt binary 2转换为十进制我们告诉第二个参数我们的数字当前是哪个基数
  • 最后,我们返回转换后的消息。

相关链接

:sunflower:中级代码解决方案:

    function binaryAgent(str) { 
      // Separate the binary code by space. 
      str = str.split(' '); 
      var power; 
      var decValue = 0; 
      var sentence = ''; 
 
      // Check each binary number from the array. 
      for (var s = 0; s < str.length; s++) { 
        // Check each bit from binary number 
        for (var t = 0; t < str[s].length; t++) { 
          // This only takes into consideration the active ones. 
          if (str[s][t] == 1) { 
            // This is quivalent to 2 ** position 
            power = Math.pow(2, +str[s].length - t - 1); 
            decValue += power; 
 
            // Record the decimal value by adding the number to the previous one. 
          } 
        } 
 
        // After the binary number is converted to decimal, convert it to string and store 
        sentence += (String.fromCharCode(decValue)); 
 
        // Reset decimal value for next binary number. 
        decValue = 0; 
      } 
 
      return sentence; 
    } 
 
    // test here 
    binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); 

:rocket: 运行代码

代码说明

  • 对于每个二进制字符串检查1并忽略零。
  • 对于那些是一个或活跃的,然后将它们转换为十进制,这考虑了它需要被提升到的位置和正确的功率。
  • 通过将功率添加到变量decValue上的任何先前变量,将功率存储到功率变量中。此变量将添加并添加活动的幂,直到循环结束,然后返回十进制数。
  • 将内部循环外部的最终小数转换为ASCII然后将其保存为句子以及已转换和存储的任何其他文本字符串。
  • 重置变量decValue以避免在继续外循环之前得到错误的小数。

相关链接

:rotating_light:高级代码解决方案

    function binaryAgent(str) { 
      return String.fromCharCode(...str.split(" ").map(function(char){ return parseInt(char, 2); })); 
    } 
 
    // test here 
    binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); 

:rocket: 运行代码

代码说明

  • 首先,我们使用split()来处理每个字符作为Array元素
  • 然后使用map()使用pareseInt()将每个元素从二进制处理为十进制
  • 最后,我们可以使用String.fromCharCode()将每个ASCII编号转换为相应的字符
  • 但是fromCharCode()需要一系列数字而不是数组我们可以使用ES6 Spread Operator将一组数字作为单独的数字传递。有关详情请参阅此处; 传播运营商

相关链接

:clipboard:捐款说明:

  • :warning: 请勿添加与任何现有解决方案类似的解决方案。如果您认为它**相似但更好** ,那么尝试合并(或替换)现有的类似解决方案。
  • 添加解决方案的说明。
  • 将解决方案分为以下类别之一 - 基本 中级高级:traffic_light:
  • 如果您添加了任何**相关的主要内容,**请仅添加您的用户名。 :warning: 不要 删除任何现有的用户名

看到:point_right: Wiki Challenge Solution Template供参考。