The code functions correctly when executed in the console, however, it encounters issues when

My code runs correctly in the console but produces incorrect results in Hackerank Issue: Determine the count of all substring palindromes within a given string

function isPalindrome(str) {

  var len = str.length;
  var mid = Math.floor(len / 2);

  for (var i = 0; i < mid; i++) {
    if (str[i] !== str[len - 1 - i]) {
      return false;
    }
  }
  
  // This function had to be implemented because I encountered an error using the built-in method.
  // Original line that caused errors:
  // str == str.split('').reverse().join('');
  
  return true;
}

function scatterPalindrome(str) {
  var result = [],
    c = 0;

  for (let i = 0; i < str.length; i++) {
    for (let j = i + 1; j < str.length + 1; j++) {
      result.push(str.slice(i, j));
    }
  }
  
  for (let i = 0; i < result.length; i++) {
    let k = result[i];
    if (isPalindrome(k))
      c++;
  }
  
  return c; // The output was consistently returning 1
}
console.log(scatterPalindrome("abc"));

input: "abc"

expected output: 3

actual output:1

Answer №1

Since I am unable to leave a comment, I will respond here by suggesting that you verify if the instructions specify multiple test cases, and for each case, run a query. In such situations, it is possible that your result may differ from theirs.

    input the number of test cases
    while the test case counter is not zero
           receive string input
           execute function to generate output for input and display it
           decrease test case counter

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to store the URLs that a user visits locally with a Chrome extension using JavaScript?

I am working on a Chrome extension that will save the URL link of the active tab when a user clicks on the extension icon. The goal is to store this URL in local storage and keep it saved until the active window is closed. I have set up an array called tab ...

Creating Low-Poly Terrain with a Touch of Serendipity

Looking to create terrain in a low-poly style inspired by the landscapes in Cube Slam and this video. I experimented with the webgl_geometry_terrain.html example, but it's still generating smooth terrain instead of the flat polygons I'm aiming fo ...

Activate the opening of a .docx file in a new tab by utilizing an anchor tag

I have attached a docx file containing the terms and conditions for our project. Now, I would like to open it in a new tab. By clicking this link, you can view the <a title="click to view terms and conditions" style="color:blue;" target="_blank" href ...

Can JavaScript code be utilized within Angular?

Hello, I am wondering if it is acceptable to include JavaScript code within Angular for DOM manipulation purposes. Here is a sample of what I have in mind: document.querySelectorAll('div.links a').forEach(function(item) { const tag = window.lo ...

Elegantly Format Haxe JS Results

Is there a way to disable the Haxe JS output that converts if statements into one-liners? It hinders step-through debugging using a map. ...

Attempting to stop an Ajax form from sending in its usual manner will not prove successful

I am currently working on implementing a form submission using Ajax. I have tried using event.preventDefault(); to prevent the form from submitting normally, but I am still being redirected to my sendMail.php file. Below is the code I am using: $(documen ...

Ways to achieve combined outcomes using ng-repeat

Check out this plunker. <div ng-repeat="subCategory in subCategorys | filter:{tags:tag}:true | orderBy:'id'"> {{subCategory.id}} {{subCategory.name}} {{subCategory.tags}} <br/><br/> The detailed information of ...

What is the best way to send an inline SVG string to my controller?

I am trying to send an inline svg string along with some other properties to my controller. When I replace the svg string with a normal string like "blabla", it successfully reaches my controller. However, with the actual svg string, it never makes it to ...

Debugging the Force-Directed D3 Graph

I stumbled upon a fantastic article that provided a detailed guide on creating a beautiful D3 force layout graph. However, I'm facing some difficulties with the JSON source: The "links" attribute in the author's JSON doesn't seem clear to m ...

Creating a custom Angular filter to group every 3 items within an iteration into a new div container

When attempting to organize three instances of app-story-preview within a wrapper div using a ngFor loop, I encountered the following code: <ng-template [ngIf]="stories.length > 0" [ngIfElse]="empty"> <cdk-virtual-scroll-viewport [itemSize ...

Looping for validation of input with strings in C++

As a newcomer to C++ with just one week of experience, I attempted to create an input validation loop that prompts the user to enter "Yes" or "No". While I was able to achieve my goal, I can't help but feel there might be a more efficient way to appro ...

Is there a method to refresh the entire DOM-based location without having to reload the browser window?

Is it possible to achieve smooth asynchronous page transitions without breaking existing JavaScript animations in a system like Webflow? I'm struggling to find a way to present new elements to the DOM without disrupting the animations that are already ...

Encountering issues with the setSinkId() function in an Electron application

I'm currently working on an app that involves playing audio and allowing users to switch between audio devices. My main issue arises when attempting to utilize the setSinkId() function - it consistently results in a DOMException AbortError with the er ...

Managing the lifecycle of a background worker in JavaScript (Node.js) - start and stop operations

I have a JavaScript function in my code that I refer to as a 'worker' which checks if it is running and performs some tasks class Application { async runWorker() { while (true) { while (!this.isStarted) ...

Javascript error when attempting to add leading zeros

Is there a way to utilize JavaScript or JQuery in order to prepend a zero to this script? for (im=1;im<=31;im++){ days[im]=everyDay[im]; } ...

Issue with displaying the glyphicon-chevron on the carousel control

Currently, I'm in the process of incorporating a straightforward modal into my gallery design. I have a slide carousel positioned at the top of the page, while at the bottom, there are thumbnails of the gallery. While the slide carousel control butto ...

What is the best way to showcase specific rows with Vue.js?

After fetching data from a specific URL, I am looking to display only the 2nd and 4th rows. { "status": "ok", "source": "n", "sortBy": "top", "articles": [ { "author": "Bradford ", "title": "friends.", ...

Is it possible to showcase D3 charts on an .epub file?

For my research project, I am exploring the possibilities of .epub files and experimenting with embedding JavaScript code to display data visualizations. I am currently using calibre to convert an HTML file containing D3 scatterplots into an .epub. The s ...

The output of Cout appears scrambled while iterating through a constant character

Upon running the code snippet below, the expected output is generated alongside some additional unexpected results: #include <iostream> using std::cout; using std::endl; int main() { const char ca[] = {'h', 'e', 'l&ap ...

Unravel the encoded string to enable JSON parsing

Here is an example of my JSON string structure [{&#034;id&#034;:0,&#034;nextCallMills&#034;:0,&#034;delay&#034;:0,&#034;start&#034;:&#034;... I am facing an issue with JSON.parseString() Even after trying unescape() a ...