Javascript program has something wrong ,I don't know why the length of log is not 10? the input s :
const s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
var findRepeatedDnaSequences = function(s) {
var set = new Set()
var seqSet = new Set()
for (let i = 0; i < s.length - 11; i ) {
let sub = s.substr(i, i 10)
console.log(sub)
if (set.has(sub)) {
seqSet.add(sub)
} else {
set.add(sub)
}
}
return Array.from(set)
};
findRepeatedDnaSequences(s)
the console result:Javascript program has something wrong ,I don't know why the length of log is not 10?
AAAAACCCCC
AAAACCCCCAA
AAACCCCCAAAA
AACCCCCAAAAAC
ACCCCCAAAAACCC
CCCCCAAAAACCCCC
CCCCAAAAACCCCCCA
CCCAAAAACCCCCCAAA
CCAAAAACCCCCCAAAAA
CAAAAACCCCCCAAAAAGG
AAAAACCCCCCAAAAAGGGT
AAAACCCCCCAAAAAGGGTTT
AAACCCCCCAAAAAGGGTTT
AACCCCCCAAAAAGGGTTT
ACCCCCCAAAAAGGGTTT
CCCCCCAAAAAGGGTTT
CCCCCAAAAAGGGTTT
CCCCAAAAAGGGTTT
CCCAAAAAGGGTTT
CCAAAAAGGGTTT
CAAAAAGGGTTT
CodePudding user response:
The arguments you're giving to substr()
are appropriate for substring()
. Since substr()
is deprecated, you should just change to substring()
and you'll get the results you want.
const s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
var findRepeatedDnaSequences = function(s) {
var set = new Set()
var seqSet = new Set()
for (let i = 0; i < s.length - 11; i ) {
let sub = s.substring(i, i 10)
console.log(sub)
if (set.has(sub)) {
seqSet.add(sub)
} else {
set.add(sub)
}
}
return Array.from(set)
};
findRepeatedDnaSequences(s)
CodePudding user response:
substr
takes a starting offset and a length. You should call just .substr(i, 10)
.
CodePudding user response:
const s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
var findRepeatedDnaSequences = function(s) {
var set = new Set()
var seqSet = new Set()
for (let i = 0; i < s.length - 11; i ) {
let sub = s.substr(i, 10)
console.log(sub)
if (set.has(sub)) {
seqSet.add(sub)
} else {
set.add(sub)
}
}
return Array.from(set)
};
findRepeatedDnaSequences(s)
CodePudding user response:
in js there are 2 functions:
substr():
The substr() method returns a portion of the string, starting at the specified index and extending for a given number of characters afterwards.
substring().
The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
you are using the 1st one, and from your question, you are expecting the 2nd.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring.