JavaScript:九九乘法表

用 js 实现一个九九乘法口诀表

1⃣️

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)

2⃣️ 实现一个中文版的输出;

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);

输出结果为:

一一得一
一二得二        二二得四
一三得三        二三得六        三三得九
一四得四        二四得八        三四一十二      四四一十六
一五得五        二五一十        三五一十五      四五二十        五五二十五
一六得六        二六一十二      三六一十八      四六二十四      五六三十        六六三十六
一七得七        二七一十四      三七二十一      四七二十八      五七三十五      六七四十二      七七四十九
一八得八        二八一十六      三八二十四      四八三十二      五八四十        六八四十八      七八五十六      八八六十四
一九得九        二九一十八      三九二十七      四九三十六      五九四十五      六九五十四      七九六十三      八九七十二      九九八十一

3⃣️

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();

4⃣️ 递归

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);

结果

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

5⃣️

function nine(num) {
	for(var i = 1;i<=num;i++) { //控制行
		var expression = "";
		for(var j = 1; j <= i; j++) { //j<=i 控制每行的个数
			expression += ${j}*${i}=${i*j};
		}
		console.log(expression);
	}
}
nine(9)

6⃣️ 脑洞大开

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
`)

7⃣️

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();

运行结果:

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

8⃣️

(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)
1 x 1 = 1   
1 x 2 = 2   2 x 2 = 4   
1 x 3 = 3   2 x 3 = 6   3 x 3 = 9   
1 x 4 = 4   2 x 4 = 8   3 x 4 = 12  4 x 4 = 16  
1 x 5 = 5   2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  
1 x 6 = 6   2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36  
1 x 7 = 7   2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49  
1 x 8 = 8   2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64  
1 x 9 = 9   2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81  

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())
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 
2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 
3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 
4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 
5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 
6*6=36 6*7=42 6*8=48 6*9=54 
7*7=49 7*8=56 7*9=63 
8*8=64 8*9=72 
9*9=81 
发布了108 篇原创文章 · 获赞 43 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/Bule_daze/article/details/104269437