Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

te #31

Closed
swrdlgc opened this issue Oct 26, 2016 · 6 comments
Closed

te #31

swrdlgc opened this issue Oct 26, 2016 · 6 comments

Comments

@swrdlgc
Copy link

swrdlgc commented Oct 26, 2016

Challenge Mutations has an issue.
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

function mutation(arr) {
  flag = [];
  for(i = 0; i < arr[0].length; ++i) {
    flag[arr[0][i]] = 1;
  }

  for(i = 0; i < arr[1].length; ++i) {    
    c = arr[1][i];
    if(flag[c] != 1 && flag[c.toUpperCase()] != 1 && flag[c.toLowerCase()] != 1) {
      return "false";
    }
  }

  return "true";
}

mutation(["hello", "hey"]);
@swrdlgc
Copy link
Author

swrdlgc commented Oct 26, 2016

solved, boolean true & false

@kinksteven
Copy link

kinksteven commented Jul 30, 2017

function mutation(arr) {
// 先全变为小写字母
var a = arr.join(" ").toLowerCase().split(" ");
//遍历,创建一个RegExp对象,在第一个元素中查找是否有第二个元素的值。
for(var i=0;i<a[1].length;i++){
var patt1 = new RegExp(a[1][i]);
//一旦找不到直接跳出,返回false。
if(patt1.test(a[0])===false){
return false;
}
}
//全部查了一遍,符合,返回true。
return true;
}

mutation(["hello", "hey"]);

因为这里数组中只有两个两个元素,所以这么写。
也能通过。
有什么不对的地方,希望大家多指正,谢谢。

第二种解法

function mutation(arr) {
// 请把你的代码写在这里
var a =arr.join(" ").toLowerCase().split(" ");
for(var i=0;i<a[1].length;i++){
var b = a[0].indexOf(a[1][i]);
if(b===-1){
return false;
}
}
return true;
}

mutation(["hello", "hey"]);

@threegeese
Copy link

var str1=arr[0].toLowerCase();
var str2=arr[1].toLowerCase().split('');
for(var i=0;i<str2.length;i++){
if(str1.indexOf(str2[i])==-1){
return false;
}else{
return true;
}
为什么不行?

@S1ngS1ng
Copy link
Contributor

@threegeese 因为你这个只会判断 str1.indexOf(str2[0]) == -1,然后就直接 return 了。
对于 false 的情况,只要这个判断成立,就可以不用继续遍历,直接返回。但 true 不同。要遍历完才能得出 true 的结论。因此,return true 放到循环外面就可以了。
参考 Profile Lookup 那道题

@promotion-xu
Copy link

promotion-xu commented Oct 26, 2018

function mutation(arr) {
// 请把你的代码写在这里

var str = arr[0]; // "hello"
var arr2 = arr[1].split(""); // ["h", "e", "y"];
for(var i = 0; i < arr2.length; i++) {
if( (str.indexOf(arr2[i].toLowerCase()) == -1) && (str.indexOf(arr2[i].toUpperCase()) == -1) ) {
// 判断条件, 把函数的第二个参数变成大写或者小写, 都要满足在str中找不到, 就直接返回false
return false;
}
}
return true;

}
mutation("hello", "hey");
判断条件写的有点复杂, 仅当参考

@ayhwtian
Copy link

function mutation(arr) {
// 请把你的代码写在这里

var a = arr[0].toLowerCase();
var b = arr[1].toLowerCase();
for(var i=0;i<b.length;i++){
if(a.indexOf(b[i])===-1){
return false;
}
}
return true;
}

mutation(["hello", "hey"]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants