Getting the last 2 characters of a dynamic string [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need your help.
One thing that's always going to be known in my function is that my string will always have a -2 at the end.
ie.
var x = filenumber-2
I'd like to use an if statement to check if the string: -2 is attached to the string. If it is just return true.
Since the filenumber value will be a variety of different combinations its length will always need to be accounted for. But as for the the -2 at the end, it is a given.

Simple regex-based solution:
if (/-2$/.test(filenumber)) return true;
Using either the String slice() or substr() methods:
if (filenumber.slice(-2) =="-2") return true;
if (filenumber.substr(-2)=="-2") return true;
The -2 in the method call means "start 2 characters before the end of the string".

if(x.slice(-2) == "-2") return true;
Will this suffice?

Use the negative operator of the slice method. It starts counting backwards from the end of the string, so it doesn't matter how long it is.
if(x.slice(-2) == "-2"){
return true;
}
Or if you really want you could use its length and count from the start
if(x.slice(x.length - 2) == "-2"){
return true;
}

Related

Why is my str.replace line not working? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm making a function that takes a string, cuts the first half (leaving middle character if odd string.length) and adds first half to end of string.
For some reason my function only partlyworks: it adds the substr to the end but doesn't cut it from the start. I tried .replace but not working.
What am I doing wrong? And/or is there a better way?
replace returns a new string with the replacement, it doesn't modify the string you call it on.
Additionally, as Pointy pointed out, you've passed the literal string 'substr' in, rather than passing in the variable substr.
So:
s = s.replace(substr, '');
a friend just gave another way to write a function that does what I wanted mine to do . I'm an amoeba and you're all wizards
function doit(s){
split = s.length /2;
if(split % 2 !== 0) { split = split-1; }
var partOne = s.slice(0, split);
var partTwo = s.slice(split + 1, s.length);
return partTwo + partOne;
}
alert(doit('123456789qwertyuio'));

Javascript String return inside script tag [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm not so good with regex or trying to return the side part of a string. Can someone help me figure this out. I have a demo below.
str = "<html><head><script>var x = '123';</script></head></html>";
console.log(str)
// should return var x = '123';
Someone wrote a very good regex for stripping tags:
var strippedStr = str.replace(/(<([^>]+)>)/ig,"");
console.log(strippedStr);
Source: http://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
I played around a bit with it and found a way with match using groups:
str.match(/(>)([^><]+)(<\/)/m)[2]
result = "var x = '123';"
=> a range (2nd group) beginning by ">" (1st group) and ending with "var x = '123';
I am not sure it'll cover all the cases...

indexOf returning -1 and 7 for the same input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
("iwsgroupware").indexOf("http://iwsgroupware"); is returning
-1
whereas ("http://iwsgroupware").indexOf("iwsgroupware"); is returning
7
Why is this so?
The two inputs are not the same.
("iwsgroupware").indexOf("http://iwsgroupware");
Since "iwsgroupware" doesn't contain the String "http://iwsgroupware", -1 is returned. On the other hand,
("http://iwsgroupware").indexOf("iwsgroupware");
"http://iwsgroupware" does contain the String "iwsgroupware", so its index (7) is returned.
Reference
First of all you have to know about indexOf().
Answer is:
returns the position of the first occurrence of a specified value in a string.
In ("iwsgroupware").indexOf("http://iwsgroupware");
iwsgroupware does't contains the string http://iwsgroupware.
So it returns -1
Where as ("http://iwsgroupware").indexOf("iwsgroupware");
http://iwsgroupware contains the string iwsgroupware.
So here indexOf() returns value 7

jQuery .click +..+ notation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to understand a codrops tutorial. Its essentially a slider with a thumb scroller.
I reached a point in the code where they were setting variables to represent clicked items in the thumb scroller like this.
var $currentTitle = $pg_title.find('h1:nth-child('+(current+1)+')');
var $nextTitle = $pg_title.find('h1:nth-child('+(idx+1)+')');
var $currentThumb = $pg_preview.find('img.pg_thumb:eq('+current+')');
I have never seen notation like this +....+. I have been digging and found examples where people used it in stack like this but I haven't seen anyone explain it could someone explain how +...+ returns the value of the clicked item?
When used with a string operand, it concatenates the two strings. In this case, to create a selector. When used with a number, it works as an addition operator. It is used in both ways here. So, if current == 1,
h1:nth-child('+(current+1)+')' will evaluate first to h1:nth-child('+2+'), which will ultimately evaluate to h1:nth-child(2)
'+' Use for string concatenation
$pg_title.find('h1:nth-child('+(current+1)+')');
Like:
var b = 'def';
If you want to add some other string in 'b' variable then you can use '+' for concatenation
var addSomotherSting ='abc' + b + 'ghi';
alert(addSomotherSting);
Then browser show a alert box with 'addSomotherSting' the out put is : 'abcdefghi'

Figure out if number is a minus or a positive number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Say I have a range which consists of < -10 and I split this up using a regex call which leaves me with < -10.
I then have a function which gets me the number from the split and I call it like range1.getMin(), this would return -10 but when I use range1.getMin().indexOf('-') it doesn't work.
Try comparing to zero:
var isNegative = range1.getMin() < 0;
function isMin(value) {
if(value<0) {
return true;
}
return false;
}
You could add a check like eval() for the value to make sure you're dealing with an integer.
Also, if you need to make sure you have a positive number (or negative number for that matter) before you use the number in your process, you can Math.abs() your number to make sure it's always a positive number.
var val = parseInt("-10", 10)
can be used to parse integer and to test for positive number
val >= 0

Categories