How to prioritize sum before string conversion in JavaScript? - javascript

Is there a way instead to do like this
"4"+4+3 which will be equal with "443" to somehow do first 4+3 like the result to be "47" (as string)? I have tried in many ways, but no one seems to work. Some ideeas? Thanks.
ps. Not switching numbers

Put the values you want to add in brackets and then concatenate
"4"+(4+3)

Use this
console.log("4"+ (4+3))

const num1=4;
const num2=3
console.log("4"+(num1+num2));
you can use the following code

Related

Getting query string from re written URL

I have url "SampleProject/profile/aA12". How can I get the value of the id from my rewritten URL using javascript? I want to get the "aA12" value.
Im using htaccess rewrite to rewrite my URL. Im new in rewritting url's. Any help will be appreciated. More powers and thank you.
You can use regex.
Try
'SampleProject/profile/aA12'.match(/\SampleProject\/profile\/(\w+)/)
'SampleProject/profile/aA12/xxx'.match(/\SampleProject\/profile\/(\w+)/)
'aA12' will be matched in both cases.
There are going to be quite a few ways to achieve your goal with JavaScript. A simple solution could be something like this:
let myURL = "SampleProject/profile/aA12";
let result = myURL.split('/').pop();
// returns "aA12"
The .split('/') method is dividing your string up into an array using the / character, and .pop() is simply returning the last element of that array.
Hope this helps! If you were looking for more advanced matching, i.e. if you wanted to ignore a potential query string on the end of the URL parameter, you could use regular expressions.
Their is a many way that you can use to achieve the desired method i made you a code pen in this link
var url = "SampleProject/profile/aA12";
let res = url.split('/').pop();
console.log(res)
https://codepen.io/anon/pen/KQxNja

How to combine many comparable find query in javascript

I made this script in javascript, it's work:
if(datatermid==16){
$('#Xhaut').find('[data-parent=16]').removeClass('active')
}
if(datatermid==17){
$('#Xhaut').find('[data-parent=17]').removeClass('active')
}
if(datatermid==18){
$('#Xhaut').find('[data-parent=18]').removeClass('active')
}
I thinks it's possible to combine all this lines into one , maybe somethink like :
$('#Xhaut').find('[data-parent=datatermid]').removeClass('active')
But it dont work...
can you please give me a solution?
Regards
Concatenate the variable using the + operator to build your selector :
$('#Xhaut').find('[data-parent='+datatermid+']').removeClass('active')
You may also do the search in one step :
$('#Xhaut [data-parent='+datatermid+']').removeClass('active')

AngularJS math joining strings instead of adding

I have a standard $scope.totals = $scope.totals = {storage:0, dailystorage:0}; and an angular.forEach that adds cam.storage to the $scope.totals.storage to give me the total storage.
I am using this to do that:
$scope.totals.storage = $scope.totals.storage+cam.storage;
The problem is that, say if two cam.storage are 21.09 and 15.82, it'll make $scope.totals.storage 21.0915.82 - basically adding them like strings instead of like math.
How do I make it an addition - not a joining?
Judging from what you've posted (verifying that $scope.totals is already a number), cam.storage is a string. You need to parse it to a number before adding it to the existing value:
$scope.totals.storage += parseFloat(cam.storage);
If they are concatenating instead of adding, it sounds like you need to parse them as decimals (You can also use toFixed(int) to limit the decimals as needed).
$scope.totals.storage = parseFloat($scope.totals.storage)+parseFloat(cam.storage);
My solution I use {{(a*1)+(b*1)}} It work.

Fix JS object replacing/encoding of strings?

I have an object with a value that has spaces in it, and it gets replaced with an encoded string, like:
alldata["test"] will return "Long+name"
or something like
alldata["test"] will return "%BLong+name%B"
when it's set by using
alldata["test"] = "Long name" (or "[Long name]") via a series of code.
Am I missing something? I don't think using $.toEvalJSON is the right way to go because I haven't transformed the object into JSON. I'd rather not do a string.replace either because I'd have to capture every possible type of input that is encoded.
Thank you!
If your question is how to remove the encoding, you could always use
unescape(s)
See Escape and Unescape Functions
The issue is related to the fact that I failed to mention that the object was being assigned the string as a result of a .serialize() command. Hence a urldecode() will work perfectly.

How to split strings

I have tried Split("?") and Split('?'). Both give the same error (link.split is not a function), so i assume that there is a way to be able to split but not using this Split?
Maybe jQuery?
Edit: Got it, it should be href.split. So as jordan said, it was not a string. So i used x.href.split("?") and it worked like a charm.
Jetpack just uses JavaScript so this should work:
var str = "My.string";
str.split("."); // => [ "My", "string" ]
If it tells you "split is not a function" then it probably means your variable doesn't hold a string like you think it does.
If the object is not already a string, convert it first.
myObject.toString().split("?");

Categories