【问题标题】:I do not why it is not roatating?我不知道为什么它不旋转?
【发布时间】:2020-05-01 03:19:46
【问题描述】:

我不知道我做错了什么。

编写一个名为 rotateArt 的函数,该函数采用两 (2) 个参数:imageArray 和旋转。 (a) imageArray 是包含 ASCII 艺术“图像”的任意维度的二维数组。 (b) 旋转是以下值之一:0、90、180、270、360、-90、-180、-270。正值表示顺时针方向的旋转。负值表示逆时针方向的旋转。值 0 或 360 表示不进行旋转,但图像将围绕垂直轴进行镜像或翻转

function rotateChar(char,rotation){
let rowLookup = ['^','v','>','<','|','-','|','\\','/','`','~','[','=','_'];
let colLookup = [-270,-180,-90,0,90,180,270];
let rotations = [
    ['>','v','<','^','>','v','<'],
    ['<','^','>','v','<','^','>'],
    ['v','<','^','>','v','<','^'],
    ['^','>','v','<','^','>','v'],
    ['-','|','-','|','-','|','-'],
    ['|','-','|','-','|','-','|'],
    ['_','|','_','|','_','|','_'],
    ['/','\\','/','\\','/','\\','/'],
    ['\\','/','\\','/','\\','/','\\'],
    ['~','`','~','`','~','`','~'],
    ['`','~','`','~','`','~','`'],
    ['=','[','=','[','=','[','='],
    ['[','=','[','=','[','=','['],
];
   if (rowLookup.indexOf(char)=== -1 || colLookup.indexOf(rotation)=== -1) {
       return char;
   } else {
       return rotations[rowLookup.indexOf(char)][colLookup.indexOf(rotation)];
   } 
   }

 function rotateArt(imageArray,rotation){
 rotation = parseInt(rotation);
 let newArray = [];
 let width = 0, height = 0;
 let cols =  [-270,-180,-90,0,90,180,270];
 let rowLookup = ['^','v','>','<','|','-','|','\\','/','`','~','[','=','_'];

switch (rotation){
 case 90:
    width = cols;
    height = rowLookup;
    for (let i =0; i < width; i++){
        let newRow = [];
        for (let j =0; j < height; j++){
        newRow.push('x');
     }
     newArray.push(newRow);
}

for (let i = 0; i < rowLookup; i++){
    for (let j =0; j < cols; j++){
        newArray[j][rowLookup - 1 - i] = rotateChar(imageArray[i][j],rotation);
    }
   }
    break;
default:
    break;
       } 
return newArray;
       }

【问题讨论】:

    标签: javascript function if-statement


    【解决方案1】:

    (例如,使用https://beautifier.io/ 之类的网站格式化您的代码可以让其他人更容易阅读,从而为您提供帮助。)

    我注意到的第一件事是错误的,您正在尝试迭代数组,如下所示:

    width = cols;
    height = rowLookup;
    for (let i = 0; i < width; i++) {
        let newRow = [];
        for (let j = 0; j < height; j++) {
            newRow.push('x');
        }
        newArray.push(newRow);
    }
    

    其中colsrowLookup,(因此widthheight)都是数组。不幸的是,Javascript 不能那样工作。

    您几乎肯定想要使用数组的长度属性。您可以更改上面示例的前两行以应用此更正:

    width = cols.length;
    height = rowLookup.length;
    

    这是另一种可行的方法

    for (let i = 0; i < cols.length; i++) {
        let newRow = [];
        for (let j = 0; j < rowLookup.length; j++) {
            newRow.push('x');
        }
        newArray.push(newRow);
    }
    

    我以前从未尝试过在数组上做更少的事情,我发现结果值得一提。以下是我尝试的一些表达式及其结果:

    0 < []
    false
    
    0 < [1]
    true
    
    0 < [1, 2]
    false
    
    0 < [0]
    false
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      相关资源
      最近更新 更多