Remember a few front-end algorithm questions

To be continued. . . .

Algorithm question one:

// JS编码实现一个render方法,使得可以这样调用:
const year = '2017';
const month = '09';
const day = '21';
const str = render('${year}-${month}-${day}')({
    
     year, month, day });
console.log(str);  // 输出2017-09-21

You can see that it is a template string. Use eval to convert the string to a template string.

    function render(format) {
    
    
      return ({
    
     year, month, day }) => eval('`' + format + '`')
    }

Algorithm question 2: (to be completed)

//  不限语言  实现一个螺旋矩阵

input = 1
output = [[1]]

input = 2
output = [[1, 2],
          [4, 3]]

input = 3
output = [[ 1, 2, 3],
          [ 8, 9, 4],
          [ 7, 6, 5]]

Algorithm question 3: (to be completed) Range is a
built-in function of some programming languages:
such as range(1,10,3), return 1, 4, 7, 10;
such as range('A','F',2), Return "A", "C", "E"
, please use Js to realize this function (ES6 can be used)

Guess you like

Origin blog.csdn.net/qq_42535651/article/details/104400701