for...in loops over the properties in the object you provide it. The properties of an array include it's indexes. Usually, its best not to use a for...in loop with an array. Instead, you might want to use a for...of loop which would give ,g,c,gcfor...in loops over the properties in the object you provide it. The properties of an array include it's indexes. Usually, its best not to use a for...in loop with an array. Instead, you might want to use a for...of loop which would give ,g,c,gcfor...in loops over the properties in the object you provide it. The properties of an array include it's indexes. Usually, its best not to use a for...in loop with an array. Instead, you might want to use a for...of loop which would give ,g,c,gc
Asked
Active
Viewed 69 times
-1
-
`for...in` loops over the properties in the object you provide it. The properties of an array include it's indexes. Usually, its best not to use a `for...in` loop with an array. Instead, you might want to use a `for...of` loop which would give `,g,c,gc` – Nick Parsons Nov 25 '19 at 01:06
-
Its syntax is unique to JS (from what I've seen). But all it does it loop through the elements in an array (rather than the indexes). So it would be similar to an enhanced for loop in Java or a regular `for in
` in python (if you are familiar with those). Here is a good post to read about the different loops in JS: https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript/9329476#9329476 – Nick Parsons Nov 25 '19 at 01:11
4 Answers
0
You are using a for..in
loop, which is for iterating objects, not arrays. Instead, use the Array.forEach()
method on the Array returned from your .split()
method call.
You should also get in the habit of ending your statements with a semi-colon and not rely on automatic semi-colon insertion.
var subsequences = function(s) {
if (s==="") {
return s;
} else {
var firstLetter = s.charAt(0);
var restOfWord = s.substring(1);
var subsequencesOfRest = subsequences(restOfWord);
var result = "";
subsequencesOfRest.split(",").forEach(function(item){
result += "," + item;
result += "," + firstLetter + item;
});
result = result.substring(1);
return result;
}
};
console.log(subsequences("gc"))

Scott Marcus
- 64,069
- 6
- 49
- 71
0
instead of using in
, you should use of
to iterate a array.
all you need to change, is one word:
var subsequences = function(s) {
if (s==="") {
return s
} else {
var firstLetter = s.charAt(0)
var restOfWord = s.substring(1)
var subsequencesOfRest = subsequences(restOfWord)
var result = ""
for (var subsequence of subsequencesOfRest.split(",")) {
result += "," + subsequence
result += "," + firstLetter + subsequence
}
result = result.substring(1)
return result
}
}
console.log(subsequences("gc"))

AlexLuo
- 458
- 1
- 4
- 12
0
Use for of
and not for in
var subsequences = function(s) {
if (s==="") {
return s
} else {
var firstLetter = s.charAt(0)
var restOfWord = s.substring(1)
var subsequencesOfRest = subsequences(restOfWord)
var result = ""
for (let subsequence of subsequencesOfRest.split(",")) {
result += "," + subsequence
result += "," + firstLetter + subsequence
}
result = result.substring(1)
return result
}
}
console.log(subsequences("gc"))

Inioluwa Sogelola
- 51
- 6
0
the point is for (var subsequence in subsequencesOfRest.split(",")) {
}
when the subsequencesOfRest is '',run subsequencesOfRest.split(","),the result is [""],
and the subsequence is index in array,so subsequence is 0.

krysliang
- 67
- 7