codewar--刷题日记

1.Multiplication Tables

Create a function that accepts dimensions, of Rows x Columns, as parameters in order to create a multiplication table sized according to the given dimensions. **The return value of the function must be an array, and the numbers must be Fixnums, NOT strings.
Example:
multiplication_table(3,3)
1 2 3
2 4 6
3 6 9
–>[[1,2,3],[2,4,6],[3,6,9]]
Each value on the table should be equal to the value of multiplying the number in its first row times the number in its first column.

solution 01

function multiplicationTable(row,col){
  out = []
  for (var i = 1; i <= row; i++)
  {
    temp = []
    // console.log(temp)
    for (var j = 1; j <= col; j++)
    {
      temp.push(i*j)
    }
    out.push(temp)
  }
  return out
}

solution 02

function multiplicationTable(row,col){
  return [...Array(row)].map((_, i) =>
    [...Array(col)].map((_, j) => (i + 1) * (j + 1))
  );
}

2.Unary function chainer

Your task is to write a higher order function for chaining together a list of unary functions. In other words, it should return a function that does a left fold on the given functions.
chained([a,b,c,d])(input)
Should yield the same result as
d(c(b(a(input))))

solution

function chained(functions) {
  return function(input) {
    return functions.reduce(function(pre, cur){
      return cur(pre)
    }, input)
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值