Get palindrome length from string [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have one string as "testaabbaccc" in this string we contain palindrome as "abba" and it's length is 4 but how can we identify this with a JavaScript code.
var string ="testaabbaccc"
Need Output as abba is palindrome and length is 4

You can use this article and modify it to your needs.
Working demo
function isPalindrome(s) {
var rev = s.split("").reverse().join("");
return s == rev;
}
function longestPalind(s) {
var maxp_length = 0,
maxp = '';
for (var i = 0; i < s.length; i++) {
var subs = s.substr(i, s.length);
for (var j = subs.length; j >= 0; j--) {
var sub_subs = subs.substr(0, j);
if (sub_subs.length <= 1)
continue;
if (isPalindrome(sub_subs)) {
if (sub_subs.length > maxp_length) {
maxp_length = sub_subs.length;
maxp = sub_subs;
}
}
}
}
return maxp;
}
console.log(longestPalind("testaabbaccc"));
console.log(longestPalind("testaabbaccc").length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Related

Javascript: Find the second longest substring from the given string Ex: I/p: Aabbbccgggg o/p: bbb [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Javascript:Find the second longest substring from given string(input and output example added in heading)
Try this
Get sequences using RegExp
Sort them based on string length
Select second Item
function getSecondSubstring(str){
let regex = new RegExp(str.toLowerCase().split("").filter((x,i,a)=>a.indexOf(x)===i).join("+|")+"+", "ig")
let substrgroups = str.match(regex);
substrgroups.sort((a,b)=> b.length-a.length);
return substrgroups[1]
}
console.log(getSecondSubstring("ööööööðððób"));
console.log(getSecondSubstring("Aabbbccgggg"));
If you don't mind using regular expressions:
function yourFunctionName(input){
let grp = input.split(/(?<=(.))(?!\1|$)/ig);
grp.sort((a,b)=> b.length-a.length);
if(grp.length <= 0){
return null;
}
else if (grp.length == 1){
return grp[0];
}
else{
grp.sort(function(a, b){
return b.length - a.length;
});
return grp[1];
}
}
console.log(yourFunctionName("ööööööðððób"));
Or another way which does not use regular expressions...
function yourFunctionName(input){
input = input.toLowerCase();
let counter = [];
let prevChar;
let countIndex = 0;
for (let index = 0, length = input.length; index < length; index++) {
const element = input[index];
if(prevChar){
if(prevChar != element){
countIndex++;
counter[countIndex] = "";
}
}
else{
counter[countIndex] = "";
}
counter[countIndex] += element;
prevChar = element;
}
if(counter.length <= 0){
return null;
}
else if (counter.length == 1){
return counter[0];
}
else{
counter.sort(function(a, b){
return b.length - a.length;
});
return counter[1];
}
}
console.log(yourFunctionName("aaaaabbbbccdd"));

Spaces in a joined array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have been working on a ceaser cipher algorithm but I haven't been able to grasp the reason why the joined array returns spaces in a peculiar state.
function rot13(str) { // LBH QVQ VG!
var string = str.split('');
var codedStr = [];
var encoded = [];
for (var k=0; k < string.length; k++){
codedStr.push(string[k].charCodeAt());
}
for(var i = 0; i < codedStr.length; i++){
if(codedStr[i] > 77 ){
codedStr[i] -= 13;
}
else if( codedStr[i] == 32 || codedStr[i] == 63){
codedStr[i] = codedStr[i];
}
else{
codedStr[i] += 13;
}
encoded.push(codedStr[i]);
}
var decode = codedStr.map(String.fromCharCode);
var result = decode.join('');
return result;
}
// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC"));
String.fromCharCode accepts multiple arguments, and map provides 3. You should use
codedStr.map(code => String.fromCharCode(code));

Need to add one by one value to comma separated List from array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to add one bye one value to comma separated list
my code
var Plist, Llist;
for (var i = 0; i < results.length; i++) {
var id = results[i].id;
if (id.startsWith("P")) {
Plist = // Add comma separated value
} else if (id.startsWith("L")) {
Llist = // add comma repeated value
}
}
please suggest better solution...
var Plist = "", Llist = "";
for (var i = 0; i < results.length; i++) {
var id = results[i].id;
if (id.startsWith("P")) {
Plist += id + ",";
} else if (id.startsWith("L")) {
Llist += id + ",";
}
}
if (Plist.indexOf(',') !== -1) {
Plist = Plist.substring(0, Plist.length - 1);
}
if (Llist.indexOf(',') !== -1) {
Llist = Llist.substring(0, Llist.length - 1);
}

Javascript substring not working as expected [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
var test = "abcdefghijklmnopqrstuvwxyz";
for(i = 0; i < test.length; i++) {
alert(test.substring(i,1));
}
I expected each alert to return each letter of the alphabet individually.
Instead, the first 5 alerts displayed as follows. Why?
a
b
bc
bcd
bcde
var test = "abcdefghijklmnopqrstuvwxyz";
for(i = 0; i < test.length; i++) {
console.log(test.substring(i,i+1));
}
actually, it's
substring(start, end)
not
substring(start, length)
unlike substr, which is indeed, substr(start, length)
If "start" is greater than "end", this method (substring) will swap the two arguments, meaning str.substring(1,4) == str.substring(4,1).
Use:
for(i = 0; i < test.length; i++) {
alert(test[i]);
}

working with css Rules using javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am finding css Rules and then changing it's style and i am done with it
but i don't know why i am getting this error
TypeError: sh[i] is undefined
if ("undefined"!==sh[i].cssRules)
my code is here
window.onload = function() {
var sh = document.styleSheets;
for (var i = 1; i <= sh.length; i++) {
if (sh[i].cssRules)
rule = sh[i].cssRules;
else if (sh[i].rules)
rule = sh[i].rules;
for (var j = 0; j < rule.length; j++) {
var sel = rule[j].selectorText;
if (sel == ".test") {
var R = rule[j].style;
R.color = "red";
}
}
}
var C = document.createElement("div");
C.className = "test";
C.innerHTML = "test";
var E = document.getElementById("div1");
E.appendChild(C);
}
i am finding all css that has been load to page and then find rule that i want to change.
if you have any question please ask me
Solved :
problem was at this line
for (var i = 1; i <= sh.length; i++) {
repalced <= withi <

Categories

Resources