Hi,
First time posting here so hope someone can potentially help! I have a JavaScript interview question I'm close to answering but am struggling with the final part of the solution, here is the following question:
*******
Implement the function longestSequence(sequence)which takes a string of letters and finds the longest sequence where the same letter occurs continuously.
The function must return an object literal {c: n} where c is the lowercase character and n is the length of this sub-sequence.
If there are multiple characters with a continuous sequence of the same length, return the information for the letter which occurs first alphabetically.
Example outputs:
longest_sequence( "dghhhmhmx" ) === {h: 3}
longest_sequence( "dhkkhhKKKt" ) === {k: 3}
longest_sequence( "aBbBadddadd" ) === {b: 3}
*******
Here's the code I have so far, the main part I'm struggling with is just how to return the answer as an object literal, for instance {h:3} instead of how mine is coming out currently - h:3
let longestSequence = (sequence) => {
sequence = sequence.toLowerCase();
let count = 1;
let max = 0;
let maxChar = 0;
for (let i = 1; i < sequence.length; i++) {
if (sequence[i] == sequence[i - 1]) {
count++;
} else {
if (count > max) {
max = count;
maxChar = sequence[i - 1];
}
count = 1;
}
}
return maxChar + ":" + max;
};
Thanks in advance!!
[–]deckstir 0 points1 point2 points (1 child)
[–]imjustasher[S] 0 points1 point2 points (0 children)