【发布时间】: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