简体   繁体   English

以及在卫兵条款中如何使用?

[英]how to use for and if in guard clause?

hello i am learning javascript and stumble upon guard clause, i have a problem that i solved normally using nested if, but when i am trying refactor the code using the code, the output its not exactly what i want. 你好,我正在学习javascript并偶然发现了警卫条款,我有一个问题,我通常使用嵌套if来解决,但是当我尝试使用代码重构代码时,其输出不完全是我想要的。 so here my usual code : 所以这是我通常的代码:

 const numberLetters = (str) => { // Code disini var temp = ''; for(var x = 0; x < str.length; x++){ if (str[x] == 1){ temp += 'i'; } else if(str[x] == 4){ temp += 'a'; } else if(str[x] == 3){ temp += 'e'; } else if(str[x] == 7){ temp += 'u'; } else if(str[x] == 0){ temp += 'o'; } else { temp += str[x]; } } return temp; } // Test cases console.log(numberLetters('s3rg31dr4g7n0v')); // sergeidragunov console.log(numberLetters('b4d41')); // badai 

and here using guard clause : 在这里使用guard子句:

 const numberLetters2 = (str) => { let temp = ''; for(var x = 0; x < str.length; x++){ if (str[x] == 1) return temp += 'i'; if (str[x] == 4) return temp += 'a'; if (str[x] == 3) return temp += 'e'; if (str[x] == 7) return temp += 'u'; if (str[x] == 0) return temp += 'o'; return temp += str[x] } } // Test cases console.log(numberLetters2('s3rg31dr4g7n0v')); // sergeidragunov console.log(numberLetters2('b4d41')); // badai 

it just get the first letter, i dont know why :( 它只是得到第一个字母,我不知道为什么:(

It gets the first letter only because after the first match it returns the letter and gets out of the function. 它仅获得第一个字母,因为在第一个匹配之后它返回字母并退出函数。 Set the values in all conditions and return in the end. 在所有条件下设置值,最后返回。 Your first way is the right way to do it 您的第一种方法是正确的方法

 const numberLetters2 = (str) => { let temp = ''; for(var x = 0; x < str.length; x++){ if (str[x] == 1) temp += 'i'; else if (str[x] == 4) temp += 'a'; else if (str[x] == 3) temp += 'e'; else if (str[x] == 7) temp += 'u'; else if (str[x] == 0) temp += 'o'; else temp+=str[x] } return temp } // Test cases console.log(numberLetters2('s3rg31dr4g7n0v')); // sergeidragunov console.log(numberLetters2('b4d41')); // badai 

How about using an object for efficient look up of the values: 如何使用对象高效查找值:

 const map = {"1": "i", "4" : "a", "3": "e", "7": "u", "0": "o"}; const numberLetters = (str) => { var temp = ''; for(var x = 0; x < str.length; x++){ temp += map[str[x]] || str[x]; //if map look up for the number succeeds add it with the temp, else take the character. } return temp; } // Test cases console.log(numberLetters('s3rg31dr4g7n0v')); // sergeidragunov console.log(numberLetters('b4d41')); // badai 

In your second code, you are returning after the first match is found or when no match is found. 在第二个代码中,您在找到第一个匹配项之后或找不到匹配项返回 So always the first letter/one letter is seen in the console. 因此,总是在控制台中看到第一个字母/一个字母。

for(var x = 0; x < str.length; x++){
    if (str[x] == 1) return  temp += 'i';  //returned from here if the match is found, subsequent code block unreachable 
    if (str[x] == 4) return  temp += 'a';
    if (str[x] == 3) return temp += 'e';
    if (str[x] == 7)  return temp += 'u';
    if (str[x] == 0) return  temp += 'o'; 
    return temp += str[x] //this will be ignored if the any of the previous statements are satisfied.
}

So, in your case you cannot use return from the if blocks as you need to check for all the cases possible. 因此,在您的情况下,您不能使用if块中的return ,因为您需要检查所有可能的情况。

For this kind of problem, you can use a switch case and for a neater iteration, you can also use for...of 对于此类问题,您可以使用开关盒,对于更整洁的迭代,您也可以将...用于

 const numberLetters2 = (str) => { let temp = ''; for(let char of str){ switch (char) { case '1' : temp += 'i'; break; case '4' : temp += 'a'; break; case '3' : temp += 'e'; break; case '7' : temp += 'u'; break; case '0' : temp += 'o'; break; default : temp += char; } } return temp; } console.log(numberLetters2('s3rg31dr4g7n0v')); // sergeidragunov console.log(numberLetters2('b4d41')); // badai 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM