JavaScript の console.log で菱型を書く
いつの間にか食レポじゃなくなっていた『俺の食べログ』さんで面白そうな問題があったので。
function diamond(length) { const height = 2 * length - 1; const width = height; const halfline = (width + 1) / 2; const space = ' '; const ast = '*'; for (var i=1; i <= height; i++) { var numOfAst = 0; var spaces = ''; var asts = ''; if (i <= halfline) { numOfAst = i*2-1; } else { numOfAst = width - 2 * (i - halfline); } spaces = space.repeat( (width - numOfAst) / 2); asts = ast.repeat(numOfAst); console.log(spaces + asts + spaces); } } diamond(10); /* * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ***************** *************** ************* *********** ********* ******* ***** *** * */