示例 1:三角形图案 - I
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 let rows = 5; // 模式变量携带字符串格式的最终模式 let pattern = ""; // 外循环针对“rows”编号运行。 次 for (let n = 1; n <= rows; n++) { // inner loop runs for n for (let num = 1; num <= n; num++) { pattern += num; } // Add a new line character after contents of each line pattern += "\n"; } console.log(pattern);
示例 2:三角形图案 - II
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 let rows = 5; // 模式变量携带字符串格式的最终模式 let pattern = ""; // 外循环针对“rows”编号运行。 次 for (let n = 1; n <= rows; n++) { // inner loop runs for n for (let num = 1; num <= n; num++) { pattern = pattern+n; } // Add a new line character after contents of each line pattern += "\n"; } console.log(pattern);
示例 3:三角形图案 - III
1 2 3 4 5 6 7 8 9 10 let rows = 4; // 变量包含模式的下一个元素 let variable = 1; // 模式变量携带字符串格式的最终模式 let pattern = ""; // 外循环针对“rows”编号运行。 次 for (let n = 1; n <= rows; n++) { for (let num = 1; num <= n; num++) { pattern += variable+" "; variable++; } pattern += "\n"; } console.log(pattern);
示例 4:数字金字塔模式 - I
1 123 12345 1234567 123456789 let rows = 5; // 模式变量携带字符串格式的最终模式 let pattern = ""; // 外循环针对“rows”编号运行。 次 for (let n = 1; n <= rows; n++) { // 内循环 - I -> 为空格 for (let space = 1; space <= rows - n; space++) { pattern += " "; } // 内循环 - II -> 数字 for (let num = 1; num <= 2 * n - 1; num++) { pattern += num; } pattern += "\n"; } console.log(pattern);
示例 5:方形星形图案 - I
***** ***** ***** ***** ***** let rows = 5; // pattern variable carries the final pattern in string format let pattern = ""; // outer loop runs for `rows` no. of times for (let n = 1; n <= rows; n++) { // Inner loop for printing stars for (let num = 1; num <= 5; num++) { pattern += "*"; } pattern += "\n"; } console.log(pattern);
示例 6:方形星形图案 - II
***** * * * * * * ***** let rows = 5; // pattern variable carries the final pattern in string format let pattern = ""; // outer loop runs for `rows` no. of times for (let n = 1; n <= rows; n++) { for (let num = 1; num <= 5; num++) { // print star only if it is the boundary location if (n == 1 || n == rows) pattern += "*"; else { if (num == 1 || num == 5) { pattern += "*"; } else { pattern += " "; } } } pattern += "\n"; } console.log(pattern);
例7:直角三角形图案
* ** *** **** ***** let rows = 5; // pattern variable carries the final pattern in string format let pattern = ""; // outer loop runs for `rows` no. of times for (let n = 1; n <= rows; n++) { // Inner loop - I -> prints spaces for (let space = 1; space <= rows - n; space++) { pattern += " "; } // Inner Loop - II -> prints stars for (let num = 1; num <= n; num++) { pattern += "*"; } pattern += "\n"; } console.log(pattern);
示例 8:金字塔三角形图案文章来源:https://www.toymoban.com/diary/mianshi/489.html
* *** ***** ******* ********* let rows = 5; // pattern variable carries the final pattern in string format let pattern = ""; // outer loop runs for `rows` no. of times for (let n = 1; n <= rows; n++) { // Inner Loop - I -> prints spaces for (let space = 1; space <= rows - n; space++) { pattern += " "; } // Inner Loop - II -> prints stars for (let num = 1; num <= 2 * n - 1; num++) { pattern += "*"; } pattern += "\n"; } console.log(pattern);
文章来源地址https://www.toymoban.com/diary/mianshi/489.html
到此这篇关于基本模式 - Javascript 面试 - 图案生成的文章就介绍到这了,更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!