-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[js] 第82天 用js实现一个九九乘法口诀表 #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
const MAX_WIDTH = 7
let table = ''
for (let rhs = 1; rhs <= 9; rhs++) {
for (let lhs = 1; lhs <= 9; lhs++) {
if (lhs <= rhs) table += `${lhs}*${rhs}=${lhs * rhs}`.padEnd(MAX_WIDTH)
}
table += '\n'
}
console.log(table) |
|
想了一下,实现一个中文版的输出; function row(num) {
const numMap = {
1: '一',
2: '二',
3: '三',
4: '四',
5: '五',
6: '六',
7: '七',
8: '八',
9: '九'
}
return new Array(num).fill(0).map((val, idx) => {
const n = idx + 1
const res = n * num
const resStr = res > 9 ? numMap[String(res)[0]] + '十' + (numMap[String(res)[1]] || '') : numMap[res]
return `${numMap[n]}${numMap[num]}${res < 10 ? '得' : ''}${resStr}`
}).join('\t')
}
const table = new Array(9).fill(0).map((val, idx) => row(idx + 1)).join('\n')
console.log(table) 输出结果为:
|
不是很想用两个 const multipleTable = () => {
const base = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const multi = [];
base.forEach((num) => {
let result = "";
multi.push(num);
multi.forEach((n) => {
result += `${n} * ${num} = ${n * num} \t`;
});
console.log(result);
});
};
multipleTable(); |
不知道为什么一遇到多层循环就喜欢用递归 function print(index) {
const arr = Array.from({ length: index })
let str = ''
arr.forEach((_, i) => (str += (`\t${i + 1} * ${index} = ${(i + 1) * index} `)))
console.log(str)
if (index === 9) return
print(++index)
}
print(1) 结果
|
来个常规打法 |
console.log(`
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
`) |
function ninenine () {
let ret = '';
for (let i = 1; i <= 9; i++) {
for (let j = 1; j <= i; j++) {
ret += `${j} × ${i} = ${j * i} `;
}
ret += `\t\n`;
}
console.log(ret);
}
ninenine(); 运行结果:
|
(function(deep) {
let str = ''
for (let i = 1; i <= deep; i++) {
for (let j = 1; j <= i; j++) {
str += `${j} x ${i} = ${j * i}`.padEnd(`${deep} x ${deep} = ${deep * deep}`.length + 2)
}
str += '\n'
}
console.log(str)
})(9)
|
function start() {
const len = 9
let output = ''
let txt = ''
for (let i = 1; i <= len; i++) {
txt = ''
for (let j = 1; j <= len; j++) {
if (i <= j) {
txt += `${i}*${j}=${i * j} `
}
}
output += txt + '\n'
}
return output
}
console.log(start())
|
我发现我真是个垃圾。。。。 |
|
function getNumberTable(){
for(let i = 1; i < 10 ; i++){
let res = ''
for(let j = 1 ; j <= i ; j++){
res += `${j}*${i}=${i*j} `
}
console.log(res + '\n')
}
}
getNumberTable() |
let res = '' |
function multipleTable () {
let str = ''
for (let i = 1; i <= 9; i++) {
for (let j = 1; j <= i; j++) {
str += `${j}*${i}=${j * i}`.padEnd(j === 1 ? 6 : 7)
}
str += '\n'
}
return str
} |
第82天 用js实现一个九九乘法口诀表
The text was updated successfully, but these errors were encountered: