Home > front end >  Encounter a difficult problem, is there any bosses to answer?
Encounter a difficult problem, is there any bosses to answer?

Time:05-15

Recently in practice front topic, encounter a difficult problem, is there who will be the problem?



Attach the answer link: primary web front-end engineer

CodePudding user response:

That the first N is not redundant ah, do not ah

CodePudding user response:

Conventional point
 
The function turns (param) {
/* * ` bearing mapping ` */
Const pathMap={E: {L: 'N' R: 'W'}, S: {L: 'E', R: 'W'}, W: {L: 'S', R: 'N'}, N: {L: 'W', R: 'E'}}
/* * ` to list ` */
Const paths=[... param toUpperCase ()]
/* * ` end result ` : the default for the north */
Let res=pathMap. N

Paths. ForEach ((path, the index)=& gt; {
/* * after ` steering towards ` */
Path=res/path
//if you need to return the location map, otherwise returns the current orientation
Res=index & lt; Paths. The length - 1. PathMap (path) : the path
//print after the turn toward
The console. The log (path, '==& gt; ', path)
})

Return res
}


This is an idea, and suddenly want to, because with the main idea is the same, just take turn to in a different way, so change up fast, make a reference
 
The function turns2 (param) {
/* * ` bearing mapping ` */
Const pathMap: any={E: {L: 'N' R: 'W'}, S: {L: 'E', R: 'W'}, W: {L: 'S', R: 'N'}, N: {L: 'W', R: 'E'}}
/* * ` to list ` */
Const paths=[... param toUpperCase ()]
/* * ` end result ` : the default for the north */
Let res=pathMap. N

While (paths. Length) {
/* * ` current steering ` : to list at the same time minus the first */
Const current=paths. The shift ()
/* * after ` steering towards ` */
Const path=res [current]

//if you need to return the location map, otherwise returns the current orientation
Res=paths. Length? PathMap (path) : the path
//print after the turn toward
The console. The log (current, '==& gt; ', path)
}
Return res
}


 
Const paths='LRR'

The console. The log (' end toward the==& gt; 'and turns (paths))
The console. The log (' end toward the==& gt; ', turns2 (paths))

CodePudding user response:

've just thought of another calculation method, writing to find the pathMap above facing east to define wrong, should be
 
Const pathMap={E: {L: 'N' R: 'S'}, S: {L: 'E', R: 'W'}, W: {L: 'S', R: 'N'}, N: {L: 'W', R: 'E'}}

The following, and also can
 
The function turns3 (param) {
/* * ` bearing list ` */
Const direction=[' E ', 'S', 'W', 'N']
/* * ` to list ` */
Const paths=[... param toUpperCase ()]
/* * ` eventually towards ` : the default for the north */
Let the index=3

Paths. ForEach (path=& gt; {
/* *
* after turn toward computing
* left: - 1; Right: + 1
Fixed: * border & lt; 0=3 (face to the east left for north);> 3=0 (for north east right)
*/
Index +={' L ': 1, "R" : 1} [path]
The index & lt; 0 & amp; & (the index=3)
The index & gt; 3 & amp; & (the index=0)
//print after the turn toward
The console. The log (path, '==& gt; 'direction [index])
})

In the return direction [index]
}
  • Related