Beginner - string.slice , no coma between parameters, and [0] written after it and i don't know why


I am working through some challenges on W3schools.

I don’t understand the end of the solution, can any body help.

Here is the webpage: JavaScript function: Check whether a passed string is palindrome or not - w3resource

The bit I don’t understand is towards the end of the solution.

if (cstr[x] != cstr.slice**(-1-x)[0]**) {

I sort of understand this saying if x is not equal to the digit of minus x then it is not a palindrome.
But i would never have thought to write it like this,

At the end of the challenge you are writing a for loop to loop through the letters of a word and then compare the first letter with the last, the second with the second to last, and so on, if they match it is a palindrome, but i dont under stand why the code is written the way it is

I would assume it would be

if (cstr[x] != cstr.slice(-1,-x)) {

where there is a coma to separate the two parameters
where ha the comma gone, do you not actually need it?

and also, why is there a [0].
Does this only relate to an array and an object… is is something to do with this. I cant work out why it needs to be there.

Thank you so much!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

.slice takes zero, one, or two parameters.

With two parameters,.slice(start, end):

  • start - The zero-based index at which to begin extraction. If negative, it is treated as str.length + beginIndex
  • end - The zero-based index before which to end extraction. The character at this index will not be included.

You wouldn’t be able to go from -1 to an earlier point in the string.

Instead, they use a single parameter,.slice(start):

  • start - The zero-based index at which to begin extraction. If negative, it is treated as str.length + beginIndex

Thank you for this, but I don’t understand at all.

so because we are starting at the end you can’t go slice(-1[start character], -3[end character]) as the -3 would not take you to the third character from the end.

Instead if you want to start at the end str.length.
I don’t understand why this is -1-x without any commas.
why does string length = -1, just because we are starting at the end?
not that we are declaring the actual length of the string?

or , is it that if you start at the end you always put -1 and then directly after the position starting from the end.

then also, why is there a [0] after the slice method.
This bit I also really don’t get!


Suppose I want to compare the fourth character and the fourth character from the end. x is 3 (remember 0 based indexing). -1-x would then be -4. Where in the string is entry -4?

As far as the [0] goes…

anyStringWhatSoEver[0] gives you the first character of anyStringWhatSoEver.

i would think -4 in the string is 4 from the end, but is that not right?
-1 last character,
-2 2nd to last character and so on.

also, why can you apply [0] to a string, does it not need to be an array?
or is it considered an object? is that why the square bracket indexing works?

sorry… been doing this for months, dont really feel like Im getting anywhere fast! appreciate your help :slight_smile:

When in doubt, play with some code:

const myCoolString = "Supercalifragilisticexpialidocious suoicodilaipxecitsiligarfilacrepus";

const x = 3;
console.log(myCoolString[x]); // Looking at 4th character

What do you see?

fabulous! thank you - I see the use of [0] now!

I’m still not really sure why there cant be a comma between the start (-1) and the end (-3).
is it because the earlier part (the end) would then be before the start.

And i still don’t understand why it is str.length = -1.
We just put that because it is the end of the string… not because we are declaring the actual length?

You can’t go to from the end to the middle of the string while scanning from left to right. So yea, you can’t go from -1 to -3.

str.length is not = -1.
-1 is just a shorter way to write “1 from the end”.

str.slice(-1) === str.slice(str.length - 1)

Thanks for all of your help :slight_smile:

On to the next challenge!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.