Comments (6)
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");
判断条件写的有点复杂, 仅当参考
from freecodecamp.cn.
solved, boolean true & false
from freecodecamp.cn.
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"]);
from freecodecamp.cn.
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;
}
为什么不行?
from freecodecamp.cn.
@threegeese 因为你这个只会判断 str1.indexOf(str2[0]) == -1
,然后就直接 return
了。
对于 false
的情况,只要这个判断成立,就可以不用继续遍历,直接返回。但 true
不同。要遍历完才能得出 true
的结论。因此,return true
放到循环外面就可以了。
参考 Profile Lookup 那道题
from freecodecamp.cn.
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"]);
from freecodecamp.cn.
Related Issues (20)
- Why I can't pass the test?
- no need to use break HOT 2
- 输入参数检验结果是正确的,但是就是通不过运行
- "25 or More" is wrong ,but I can't fix it. HOT 1
- "9 or Under" is wrong ,but I can't fix it. HOT 1
- why i can't pass Create a Bootstrap Row, pls help?
- Truncate a string 有点问题
- Add your JavaScript Slot Machine Slots 文字错误
- Build a Tribute Page |观看不了右边视频,是否有其他视频源 HOT 1
- 在style标签的最后两段代码,按照要求添加的Id选择器,代码无法通过,并无明显错误,不知道什么原因?
- The Code I write wrong deliberately can run and work as well. HOT 1
- 代码输出正确,却不能通过测试 I'm a little tea pot HOT 1
- 点击校验
- input中type属性checkbook无法识别
- 这个为什么不能过去呢
- Chinese interface:It allows data structures to be any combination of strings, numbers, Booleans, strings, and objects.Did you write "strings" again?
- 这个是BUG吗 HOT 1
- 这样为什么会显示错误
- 为什么过不去?例题和实际无联系。 HOT 1
- 哪里有“我已经完成了这个挑战按钮”啊
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freecodecamp.cn.