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.
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.
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
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?