Searching for empty string in str.indexOf() in javascript - javascript

Searching for the empty string using String.indexOf(searchValue[,fromindex]) returns unexpected results as given in the following snippet.
console.log('foo'.indexOf('')) // 0
console.log('foo'.indexOf('',2)) // 2
console.log('foo'.indexOf('',3)) // 3
console.log('foo'.indexOf('',4)) // 3
console.log('foo'.indexOf('',100)) // 3

It searches for the first match, starting at the position given with the second argument (the fromIndex - if any). But the empty string will always be matched as soon as possible; it's an empty string, after all. When you provide a fromIndex greater than the length of the string, it simply runs the test as if the fromIndex was the length of the string:
An integer representing the index at which to start the search; the default value is 0. For fromIndex values lower than 0 or greater than str.length, the search starts at index 0 and str.length respectively.

Empty string exist everywhere, for example for your first log, the empty string exist in first of string, you can think of "foo" as "" + "foo", same as other logs. basically indexOf returns the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

Related

Why slice(-x,0) doesn't produce expected string

If I have this:
console.log('12345'.slice(-3)); // I get '345'
but when I do:
console.log('12345'.slice(-3, 0)); // I get '' (empty string).
what is the reasoning behind this? It makes no sense to me. Is there a JS API that can wrap around strings like I expect here? (I was expecting '345' again.)
The second parameter is the index where the substring to be extracted ends (exclusive). If that index is before (or equal to) the start index (the first parameter), the result is the empty string.
As the docs say about the second parameter:
If end is positioned before or at start after normalization, nothing is extracted.
After normalization, .slice(-3) is equivalent to .slice(2), and the second parameter defaults to the length of the string, so
.slice(-3)
// equivalent to
.slice(2)
is like
.slice(2, 5) // take the substring from index 2 to index 5
and not
.slice(2, 0)

Understanding the .length property

Could someone please explain what the .length property is doing in the following code:
let sentenceCount = 0;
betterWords.forEach(word => {
if (word[word.length-1] === '.' || word[word.length-1] === '!') {
sentenceCount++;
}
});
I understand the basic idea of what .length does, but when I try to print out word[word.length], it prints out as undefined. If I print out word[word.length-1], then I get the . and ! in the text. I'm not understanding what word[word.length-1] actually is so that when -1 is attached it gets the characters on the end.
Thank you in advance for any advice on this.
JavaScript exhibits zero based indexing, that is, the first element in an array or in a string is at position 0. Therefore, an array or string of length n has elements going from position 0 to n - 1, with element at position n being undefined. This means that an array or string with n elements has the last element at n - 1, which is accessed as someArrayString[n - 1].
.length returns the length of an array or a string. Hence, the last element of an array or a string is found at someArrayString.length - 1 which is accessed as someArrayString[someArrayString.length - 1].
From the code, it can be inferred that word is a string. Therefore, the line word[word.length-1] accesses the last char (letter) in the word (although it actually accesses the last code unit
but in ASCII a code unit correspond with a 1 byte ASCII char).
For example, the string var word = "JavaScript" has length 10. With J at position 0 and t at position 9. In other words, word[0] == 'J' and word[word.length - 1] == 't'
Let's say your word = 'People';
word.length would return 6 which is the character number in your word.
Since arrays (in this case string because .length can be used in strings too) start from index 0, word.length-1 would give you the 5th element of your string, which is the last character of your word.
In your code, if (word[word.length-1] === '.' || word[word.length-1] === '!') checks if the last character of a word is a dot (.) or exclamation point (!) so you can count how many sentences there are in a given string.
word.length-1 - Returns the last index in the word
Explanation
word= 'test!';
console.log(word[word.length-1])
OUTPUT - !
The Length of word = 5
We need to get the last index of the word i.e word[4] as the array indexing starts from 0
Your .length property returns the length of the string. The string itself is a one-dimensional array with elements of the type character so using .length gives the size of the array. Arrays use zero-based indexing.
So in your case word[word.length-1] returns the last element/character of the array and word[word.length] goes beyond the bounds of the array and that's why it prints out undefined.
Here's a playground you can test it.
const string = "Just a test.";
//get the last element
console.log(string[string.length-1]);
//trying to get an element outside the bounds of the array
console.log(string[string.length]);
Alright, In your code I assume that betterWords is an array because you are using forEach.
And also assume the betterWords array is something like,
betterWords = ['word.','words!','hello','world'];
Length Property
The length property returns the length of array or string.
let myArray = [0,5,6,8,9];
let myString = 'hello';
console.log(myArray.length) // 5 | because myArray has total 5 elements.
console.log(myString .length) // 5 | because myString has total 5 characters.
Index
Then index tells us the position of an element in an array or a character in a string.
The index is starting from 0.
let myString = 'hello';
/*
* h e l l o
* - - - - -
* 0 1 2 3 4
*/
console.log(myString[0]); // h | because h is 0 index of hello
So now you can see the length of the word hello is 5. But the last index of the word hellois4`. Which means the last index = length - 1
That's why here we are subtracting 1 from length to get the last index.
console.log(word[word.length-1]);

I need an explanation on slice()

I was completing javascript exercises and got stuck on creating a function which checks if a string is a palindrome. For your reference, this was the given solution.
I understand what slice() does as a method, and I followed the function here until the point where it checks to see if the first letter matches the last.
if (cstr[x] != cstr.slice(-1-x)[0])
What is going on with the parameter values of the slice method? I was expecting the parameters to slice the last letter and I presume it does but I would appreciate some help in pointing out how!
Let's pop open the console and take a look!
> var s = 'abcde'
"abcde"
> s[0]
"a"
> s.slice(-1)
"e"
> s.slice(-1)[0]
"e"
> s[1]
"b"
> s.slice(-1 - 1)
"de"
> s.slice(-1 - 1)[0]
"d"
As I hope you can see by now, slicing with a negative number -n will return the last n characters of the string, with n = 1 being the last character. So to have the palindrome test work correctly, you need to slice at -1 - x (the "x-th from last" character).
arr.slice([begin[, end]])
Parameters
begin
Zero-based index at which to begin extraction. As a negative index, begin indicates an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence. If begin is omitted, slice begins from index 0.
end
Zero-based index at which to end extraction. slice extracts up to but not including end. slice(1,4) extracts the second element up to the fourth element (elements indexed 1, 2, and 3). As a negative index, end indicates an offset from the end of the sequence. slice(2,-1) extracts the third element through the second-to-last element in the sequence. If end is omitted, slice extracts to the end of the sequence (arr.length).
Description
slice returns a shallow copy of elements from the original array. Elements of the original array are copied into the returned array as follows:
For object references (and not the actual object), slice copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
For strings and numbers (not String and Number objects), slice copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other array.
If a new element is added to either array, the other array is not affected.

javascript, parseInt behavior when passing in a float number

I have the following two parseInt() and I am not quite sure why they gave me different results:
alert(parseInt(0.00001)) shows 0;
alert(parseInt(0.00000001)) shows 1
My guess is that since parseInt needs string parameter, it treats 0.00001 as ""+0.00001 which is "0.00001", therefore, the first alert will show 0 after parseInt. For the second statement, ""+0.00000001 will be "1e-8", whose parseInt will be 1. Am I correct?
Thanks
I believe you are correct.
parseInt(0.00001) == parseInt(String(0.00001)) == parseInt('0.00001') ==> 0
parseInt(0.00000001) == parseInt(String(0.00000001)) == parseInt('1e-8') ==> 1
You are correct.
parseInt is intended to get a number from a string. So, if you pass it a number, it first converts it into a string, and then back into a number. After string conversion, parseInt starts at the first number in the string and gives up at the first non-number related character. So "1.e-8" becomes "1"
If you know you are starting with a string, and are just trying to get an Integer value, you can do something like.
Math.round(Number('0.00000001')); // 0
If you know you have a floating point number and not a string...
Math.round(0.00000001); // 0
You can also truncate, ceil(), or floor the number
parseInt takes each character in the first argument (converted to a string) that it recognizes as a number, and as soon as it finds a non-numeric value it ignores that value and the rest of the string. (see MDN second paragraph under "Description")
Therefore it's likely that parseInt(0.00000001) === parseInt(String(0.00000001)) === parseInt("1e-8"), which would only extract the 1 from the string yielding parseInt("1") === 1
However, there's another possibility:
From Mozilla developer network:
parseInt(string, radix);
for the string argument (emphasis added): "The value to parse. If string is not a string, then it is converted to one. Leading whitespace in the string is ignored."
I think this possibility is less likely, since String(0.00000001) does not yield NAN.

What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods?
String.prototype.slice
String.prototype.substring
slice() works like substring() with a few different behaviors.
Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);
What they have in common:
If start equals stop: returns an empty string
If stop is omitted: extracts characters to the end of the string
If either argument is greater than the string's length, the string's length will be used instead.
Distinctions of substring():
If start > stop, then substring will swap those 2 arguments.
If either argument is negative or is NaN, it is treated as if it were 0.
Distinctions of slice():
If start > stop, slice() will return the empty string. ("")
If start is negative: sets char from the end of string, exactly like substr() in Firefox. This behavior is observed in both Firefox and IE.
If stop is negative: sets stop to: string.length – Math.abs(stop) (original value), except bounded at 0 (thus, Math.max(0, string.length + stop)) as covered in the ECMA specification.
Source: Rudimentary Art of Programming & Development: Javascript: substr() v.s. substring()
TL;DR:
If you know the index (the position) on which you'll stop (but NOT include), use slice().
If you know the length of characters to be extracted, you could use substr(), but that is discouraged as it is deprecated.
Otherwise, read on for a full comparison
Syntax
string.slice(start,end)
string.substr(start,length)
string.substring(start,end)
Note #1: slice()==substring()
What it does?
slice() extracts parts of a string and returns the extracted parts in a new string.
substr() extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
substring() extracts parts of a string and returns the extracted parts in a new string.
Note #2: slice()==substring()
Changes the Original String?
slice() doesn't
substr() doesn't
substring() doesn't
Note #3: slice()==substr()==substring()
Using Negative Numbers as an Argument
slice() selects characters starting from the end of the string
substr() selects characters starting from the end of the string
substring() doesn't perform
Note #4: slice()==substr()
If the First Argument is Greater than the Second
slice() doesn't perform
substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems
substring() will swap the two arguments, and perform as usual
The First Argument
slice() required; starting Index
substr() required; starting Index
substring() required; starting Index
Note #5: slice()==substr()==substring()
The Second Argument
slice() optional; the position (up to, but not including) where to end the extraction
substr() optional; the number of characters to extract
substring() optional; the position (up to, but not including) where to end the extraction
Note #6: slice()==substring()
What if the Second Argument is Omitted?
slice() selects all characters from the start-position to the end of the string
substr() selects all characters from the start-position to the end of the string
substring() selects all characters from the start-position to the end of the string
Note #7: slice()==substr()==substring()
So, you can say that there's a difference between slice() and substr(), while substring() is basically a copy of slice().
If you want substr's functionality:
"foobarbaz".substr(index, length);
without using a deprecated feature, you can just do:
"foobarbaz".substring(index, length + index);
And get the exact same results bar all of the edge-cases, like negative index/length.
Ben Nadel has written a good article about this, he points out the difference in the parameters to these functions:
String.slice( begin [, end ] )
String.substring( from [, to ] )
String.substr( start [, length ] )
He also points out that if the parameters to slice are negative, they reference the string from the end. Substring and substr doesn't.
Here is his article about this.
The one answer is fine but requires a little reading into. Especially with the new terminology "stop".
My Go -- organized by differences to make it useful in addition to the first answer by Daniel above:
1) negative indexes. Substring requires positive indexes and will set a negative index to 0. Slice's negative index means the position from the end of the string.
"1234".substring(-2, -1) == "1234".substring(0,0) == ""
"1234".slice(-2, -1) == "1234".slice(2, 3) == "3"
2) Swapping of indexes. Substring will reorder the indexes to make the first index less than or equal to the second index.
"1234".substring(3,2) == "1234".substring(2,3) == "3"
"1234".slice(3,2) == ""
--------------------------
General comment -- I find it weird that the second index is the position after the last character of the slice or substring. I would expect "1234".slice(2,2) to return "3". This makes Andy's confusion above justified -- I would expect "1234".slice(2, -1) to return "34". Yes, this means I'm new to Javascript. This means also this behavior:
"1234".slice(-2, -2) == "", "1234".slice(-2, -1) == "3", "1234".slice(-2, -0) == "" <-- you have to use length or omit the argument to get the 4.
"1234".slice(3, -2) == "", "1234".slice(3, -1) == "", "1234".slice(3, -0) == "" <-- same issue, but seems weirder.
My 2c.
The difference between substring and slice - is how they work with negative and overlooking lines abroad arguments:
substring(start, end)
Negative arguments are interpreted as zero. Too large values ​​are truncated to the length of the string:
alert("testme".substring(-2)); // "testme", -2 becomes 0
Furthermore, if start > end, the arguments are interchanged, i.e. plot line returns between the start and end:
alert("testme".substring(4, -1)); // "test"
// -1 Becomes 0 -> got substring (4, 0)
// 4> 0, so that the arguments are swapped -> substring (0, 4) = "test"
slice
Negative values ​​are measured from the end of the line:
alert("testme".slice(-2)); // "me", from the end position 2
alert("testme".slice(1, -1)); // "estm", from the first position to the one at the end.
It is much more convenient than the strange logic substring.
A negative value of the first parameter to substr supported in all browsers except IE8-.
If the choice of one of these three methods, for use in most situations - it will be slice: negative arguments and it maintains and operates most obvious.
substr: It's providing us to fetch part of the string based on specified index.
syntax of substr-
string.substr(start,end)
start - start index tells where the fetching start.
end - end index tells upto where string fetches. It's optional.
slice: It's providing to fetch part of the string based on the specified index. It's allows us to specify positive and index.
syntax of slice - string.slice(start,end)
start - start index tells where the fetching start.It's
end - end index tells upto where string fetches. It's optional.
In 'splice' both start and end index helps to take positive and negative index.
sample code for 'slice' in string
var str="Javascript";
console.log(str.slice(-5,-1));
output: crip
sample code for 'substring' in string
var str="Javascript";
console.log(str.substring(1,5));
output: avas
[*Note: negative indexing starts at the end of the string.]
The only difference between slice and substring method is of arguments
Both take two arguments e.g. start/from and end/to.
You cannot pass a negative value as first argument for substring method but for slice method to traverse it from end.
Slice method argument details:
Arguments
start_index
Index from where slice should begin. If value is provided in negative it means start from last. e.g. -1 for last character.
end_index
Index after end of slice. If not provided slice will be taken from start_index to end of string. In case of negative value index will be measured from end of string.
Substring method argument details:
Arguments
from
It should be a non negative integer to specify index from where sub-string should start.
to
An optional non negative integer to provide index before which sub-string should be finished.
For slice(start, stop), if stop is negative, stop will be set to:
string.length – Math.abs(stop)
rather than:
string.length – 1 – Math.abs(stop)

Categories

Resources