Javascript is concating or appending instead of adding [duplicate] - javascript

This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 5 years ago.
javascript function:
//from javascript file1
for(i=0;i<10;i++){
setContent(i);
}
//from javascript file2 where function is present
setContent: function (content) {
var xy=0;
xy = parseInt(xy + content);
console.log(xy);
Above code is appending the content instead of adding

do,
xy = xy + parseInt(content);
instead of,
xy = parseInt(xy + content);
as by doing this xy is concatenated first then is parsed to integer

Related

How to replace url in js whihot jquery? [duplicate]

This question already has answers here:
js function to get filename from url
(24 answers)
Closed 6 years ago.
Guys i have many links:
"img/dasda/ja.png", "../img/no.png", "wild/rpm/gulp/in.png",
How in js change sting to last "/"? To get just the name of image?
var url = "img/dasda/ja.png";
var idx = url.lastIndexOf('/');
console.log(url.substring(idx + 1));
You could use Array#match together with RegExp.
var links = ["img/dasda/ja.png", "../img/no.png", "wild/rpm/gulp/in.png"];
links.forEach(v => console.log(v.match(/(?!\/)\w+(?=\.)/g)[0]));

can't remove parameter using javascript [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 6 years ago.
i have a url like this
test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14
i want to remove the parameter using the following javascript
function removeParam(uri) {
uri = uri.replace(/([&\?]start_date=*$|start_date=*&|[?&]start_date=(?=#))/, '');
return uri.replace(/([&\?]end_date=*$|end_date=*&|[?&]end_date=(?=#))/, '');
}
but it didn't work, anyone know what's wrong with that?
in modern browsers you can do this quite simply
var x = new URL(location.origin + '/test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14');
x.searchParams.delete('start_date');
x.searchParams.delete('end_date');
var uri = x.pathname.substr(1) + x.search; // substr(1) because you don't have a leading / in your original uri
at least, I think it's simpler
Your RegExp is no match!
If you want remove end_date, you should:
uri.replace(/(end_date=)([^*&]+)/, 'end_date=')
And so on.

Not able to `replace` content in `style` - where the `html` is stored in variable [duplicate]

This question already has answers here:
remove / replace unwanted prefixed info from `class` name
(2 answers)
Closed 7 years ago.
I am trying to update the style html - by replacing a string, but not working. i am not able to get the final updated html.
here is my try:
var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>"
html += $(html).html(function(_,h){
$(h).find('style').html().replace(/^\w+ (\.\w+)/gm,'$1');
});
console.log(html); //throws the error.
jsfiddle
It works for me..
var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>";
var $h = $('<div>'+html+'</div>');
$h.find('style').html(function(_,h){ return h.replace(/^\w+ (\.\w+)/gm,'$1'); });
var updated = $h.html();
console.log(updated);
Thanks every one!

How to insert commas into the result of a javascript function [duplicate]

This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 8 years ago.
I've been trying to figure out how to insert commas into the result of this function for an hour now.
var i = 2574672248;
function increment2() {
i++;
document.getElementById('xxx').innerHTML = i;
}
setInterval('increment2()', 50);
The result is incremented by 1 every half a second. I'm trying to make it so, that if the result is 2394672248 for example, it's shown as "2,574,672,248" instead of the raw "2574672248". I've tried incorporating different comma inserting functions into it, but it's not working.
var i = 2574672248;
function increment2() {
i++;
document.getElementById('xxx').innerHTML = i.toLocaleString('en');
}
setInterval('increment2()', 50);
From here: add commas to a number in jQuery

Get the last occurrence of string within another string [duplicate]

This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
Is there a javascript function to get the last occurrence of a substring in a String
Like:
var url = "/home/doc/some-user-project/project";
I would like a function that returns true if the String contains project at his end.
I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?
Thanks for the answer
Something like
var check = "project",
url = "/home/doc/some-user-project/project";
if (url.substr(-check.length) == check){
// it ends with it..
}
Try this
<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>
The response is a array with project, if the responde is 'null' because it is not found

Categories