Skip to content

[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

Open
haizhilin2013 opened this issue Jul 6, 2019 · 15 comments
Open

[js] 第82天 用js实现一个九九乘法口诀表 #582

haizhilin2013 opened this issue Jul 6, 2019 · 15 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

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

@haizhilin2013 haizhilin2013 added the js JavaScript label Jul 6, 2019
@ghost
Copy link

ghost commented Jul 7, 2019

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)

@Ardanas
Copy link

Ardanas commented Jul 7, 2019

for(let i = 1; i<10; i++){ for(let j = 1; j<i+1; j++){ document.write(${i} * ${j} = ${i*j}) document.write("&nbsp;&nbsp;&nbsp;&nbsp;") } document.write("<br>") }

@xxf1996
Copy link

xxf1996 commented Jul 7, 2019

想了一下,实现一个中文版的输出;

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)

输出结果为:

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

@Konata9
Copy link

Konata9 commented Jul 8, 2019

不是很想用两个 for 循环,凑了个数组……但感觉还是在 for 循环(T T)

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

@imccode
Copy link

imccode commented Jul 9, 2019

不知道为什么一遇到多层循环就喜欢用递归

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

@songguoguo927
Copy link

来个常规打法
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)

@ouyinheng
Copy link

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

@xiaotianxia
Copy link

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 

@diandianzd
Copy link

(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  

@rni-l
Copy link

rni-l commented Jan 22, 2020

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 

@songlovena
Copy link

我发现我真是个垃圾。。。。
var content = '';
for(var i=1; i<=9; i++){
var tr = '';
for(var j=1; j<=i; j++){
var temp = j+''+i+'='+ji;
var td = '' + temp + '';
tr += td;
}
tr += '';
content += tr;
}
document.getElementById('tb').innerHTML = content;

@smile-2008
Copy link

不知道为什么一遇到多层循环就喜欢用递归

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

@1684838553
Copy link

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

@xiaoqiangz
Copy link

let res = ''
for(let i=1;i<10;i++) {
for(let j=1;j<=i;j++) {
res +=${i} * ${j} = ${i*j}
}
res += '\n'
}
console.log(res)

@panpanxuebing
Copy link

panpanxuebing commented Dec 16, 2024

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests