Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created August 29, 2014 00:10
Show Gist options
  • Save jikeytang/a1b880f82c5c6145aa39 to your computer and use it in GitHub Desktop.
Save jikeytang/a1b880f82c5c6145aa39 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140829-题目1
用最优的代码实现:
如何判断出字符串 b 中的每一个字符,在 a 中是否存在。
比如:
var a = 'lkjgfaoids';
var b = 'adc';
ad存在,c不存在。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@wsgouwan
Copy link

            var a = 'lkjgfaoids';
            var b = 'adc';

            a = a.split('');
            b = b.split('');

            var arr1 = a.filter(function(v) { return b.indexOf(v) > -1; })  //  保存存在的
            var arr2 = b.filter(function(v) { return arr1.indexOf(v) == -1; })

            console.log( arr1.join('')+ "存在," + arr2.join('')+"不存在"  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment