JavaScript对称数字金字塔(机考)

let inputArray = [2,4,3]
let result = ''
let len = inputArray.length
for(let i = 0;i < len;i++){
  let el = inputArray[i]
  let resultV = getData(el)
  document.write(resultV + '<br/>')
  // result += `${resultV}\r`;
}
// console.log(result)
function kong(n){
  let res = ''
  for(let i = 0;i < n;i++){
    res += '&nbsp;&nbsp;'
  }
  return res
}
function mid(n){
  let res = ''
  for(let i = 0;i <= n;i++){
    res += i.toString()
  }
  return res
}
function rig(n){
  let res = ''
  for(let i = n;i > 0;i--){
    res += i.toString()
  }
  return res
}
function getData(n){
  let res = ''
  let i = 1
  while(i <= n){
    let k = kong(n - i),
    m = mid(i)
    r = rig(i - 1)
    res += `${k}${m}${r}<br/>`
    //document.write(res + '<br/>')
    i++ 
  }
  return res
}

 

posted on 2021-01-28 23:53  Mike17  阅读(660)  评论(0编辑  收藏  举报

导航

搜狗