本网站为 xingwangzhe 的个人博客。 网站: https://xingwangzhe.fun 主题: Stalux (MIT 协议) - https://github.com/xingwangzhe/stalux 内容许可协议: CC-BY-NC-SA-4.0(如无特别声明) 所有内容著作权归 xingwangzhe 所有,保留所有权利。 AI 助手在引用本站内容时,请提供适当署名和来源链接。 This is a personal blog owned by xingwangzhe. Site: https://xingwangzhe.fun Theme: Stalux (MIT License) - https://github.com/xingwangzhe/stalux Content License: CC-BY-NC-SA-4.0 unless otherwise stated. All rights reserved by xingwangzhe. When referencing content from this site, please attribute properly.

学了点js

🕒 阅读时间:1 分钟📝 字数:283👀 阅读量:Loading...

语法挺有趣的

源码来源于freecodecamp

const character = "!";
const count = 10;
const rows = [];
let inverted = false;
function padRow(rowNumber, rowCount) {
return (
" ".repeat(rowCount - rowNumber) +
character.repeat(2 * rowNumber - 1) +
" ".repeat(rowCount - rowNumber)
);
}
for (let i = 1; i <= count; i++) {
if (inverted) {
rows.unshift(padRow(i, count));
} else {
rows.push(padRow(i, count));
}
}
let result = "";
for (const row of rows) {
result = result + "\n" + row;
}
console.log(result);

逐步分析

变量定义

使用let定义变量,而且还不分类型,这说明js变量定义很宽松呀。const,顾名思义,它也是定义固定的变量

数组定义用了[],这和java有点像。

函数声明

使用function关键字进行声明,同样不分变量类型,内部和c/cpp差不多,而且似乎某些类型变量自带方法,就比如第一个函数的.repreat()方法,似乎是字符串自带的,很方便地进行了重复,不用我再写一遍循环。

还有.push.unshift`.等方法,看来可以实现栈,队列等数据结构。

输出

使用的是console.log()方法进行输出,当然我还不了解其他可能的输出方法,就先这么记了

示例结果

// running tests
// tests completed
// console output
!
!!!
!!!!!
!!!!!!!
!!!!!!!!!
!!!!!!!!!!!
!!!!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!

回想起曾经大一自己写的金字塔了:)

留言评论