javascript 试图在codewars上完成一个kata,我的代码通过了所有给定的测试,除了随机测试,它搞乱了我的结果?

xkrw2x1b  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(103)

这是我的第一次!
这是我的解决方案。。对不起的格式和大小,但它的工作,或应该,我不知道为什么?

`function blockPrint(input){
  if (input.trim()=="") return"";
const string=input.trim().toUpperCase();    //trims off spaces and converts to uppercase
const map=[];
let output="";            // Uses the exact requirements given in expected output as shown below 
let pos=0;
map[0]=" AAA  BBBB   CCC  DDDD  EEEEE FFFFF  GGG  H   H IIIII JJJJJ K   K L     M   M N   N  OOO  PPPP   QQQ  RRRR   SSS  TTTTT U   U V   V W   W X   X Y   Y ZZZZZ";
map[1]="A   A B   B C   C D   D E     F     G   G H   H   I       J K  K  L     MM MM NN  N O   O P   P Q   Q R   R S   S   T   U   U V   V W   W X   X Y   Y     Z";
map[2]="A   A B   B C     D   D E     F     G     H   H   I       J K K   L     M M M N   N O   O P   P Q   Q R   R S       T   U   U V   V W   W  X X   Y Y     Z ";  
map[3]="AAAAA BBBB  C     D   D EEEEE FFFFF G GGG HHHHH   I       J KK    L     M   M N N N O   O PPPP  Q   Q RRRR   SSS    T   U   U V   V W W W   X     Y     Z  ";
map[4]="A   A B   B C     D   D E     F     G   G H   H   I       J K K   L     M   M N   N O   O P     Q Q Q R R       S   T   U   U V   V W W W  X X    Y    Z   ";  
map[5]="A   A B   B C   C D   D E     F     G   G H   H   I       J K  K  L     M   M N  NN O   O P     Q  QQ R  R  S   S   T   U   U  V V  W W W X   X   Y   Z    ";  
map[6]="A   A BBBB   CCC  DDDD  EEEEE F      GGG  H   H IIIII JJJJ  K   K LLLLL M   M N   N  OOO  P      QQQQ R   R  SSS    T    UUU    V    W W  X   X   Y   ZZZZZ";

 

  
  for (let i=0;i<7;i++){
  for(let j=0;j<string.length-1;j++){
    pos=(string.charCodeAt(j)-65)*6;    // works out which bit to add     
    if (pos=>0 && pos<=91){                        // and if its not a space
     output+=(map[i].slice(pos,pos+6)); // adds it
      }                                      //just do up till the last char because of test requirement for no trailing spaces
    
    if((pos/6)+65<65) {                // or if it is a space
        output+="      ";                // adds a space of 6 "spaces"
      }
            
  }
    pos=(string.charCodeAt((string.length)-1)-65)*6;        // finds the last char
    output+=(map[i].slice(pos,pos+6)).trimEnd();        // adds the bit with its "end" trimmed off as per the test requirements
    
 if(i<6) {output+="\n";}   //this to ensure no added carriage returns are added at the end as per test requirements 
  
} 
  
  
 console.log(output); 
  return output;
  
}`

我尝试了上面的代码,它通过了所有给定的测试,但由于一些奇怪的原因,当给定“30个随机字符”时(这是任何高达30)它要么看起来应该通过(控制台日志显示与预期输出匹配)或者它通过从结果中删除白色或添加随机字符负载来搞乱结果??我“尝试”的次数越多,错误似乎就越多。我问这是怎么回事,有人把我的问题标记为“包含剧透”??我猜是因为这个原因。我错过了一些关于javascript如何处理数据的基本知识,或者codewars测试的工作方式?

qvtsj1bj

qvtsj1bj1#

通过比较工具运行您的输出与相同的输出,字母“Z”似乎有问题。
仔细观察一下,似乎字符串中的每个字符后面都有一列空格,除了Z。

相关问题