javascript - 我不明白为什么它不旋转?

标签 javascript function if-statement

我不知道我做错了什么。

编写一个名为rotateArt 的函数,该函数采用两(2) 个参数:imageArray 和rotation。 (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;
       }

最佳答案

(例如使用 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

关于javascript - 我不明白为什么它不旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61536317/

相关文章:

javascript - 使用 Protractor 测试 UI-bootstrap-alert

javascript - 使用 JS 模糊将焦点转移到 div 上

c# - 在 API 中对参数验证抛出异常是否被认为是一种好的做法?

c - 我该如何修复 C 中的 if 语句以使其按预期工作?

r - 在 R 中使用带有条件的 ifelse 进行变异

javascript - Vanilla JavaScript Flip Card 游戏无法运行

function - LISP - 无法使用可选参数调用函数

function - 在Kotlin中调用函数 “Double.foo()”

Javascript 变量在不应该为 true 时变为 true

javascript - FullCalendar 单击编辑资源