Unable to add two variables in javascript [duplicate] - javascript

This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
How to force addition instead of concatenation in javascript [duplicate]
(3 answers)
Closed 12 days ago.
I am working with javascript and right now trying to sum(add/plus) two variables but right now instead of "addition"(plus) variables are "concate",Here is my current code
var first = this.value; // getting value "5"
var second =$("#earning").val(); // getting value "2"
var final_value =first + second;
M
y expected result is "7"(5+2) but its giving me "52",how can i fix this ?

Related

How to get the one before last element of an array in JavaScript? [duplicate]

This question already has answers here:
get the second to last item of an array?
(11 answers)
Closed 10 months ago.
I wanna to get one before last element of array in JavaScript. but in console, when I use this code "array.length -2"dose not work, and it just return me the length of array minus 2.
and when I use this code "array.at[-2]" it returns "undefined"!
const arr = [1,2,3,4,5,6]
console.log(arr[arr.length -2])
//5

Is there a random function in javascript that doesn't take the range of two numbers? [duplicate]

This question already has answers here:
Getting a random value from a JavaScript array
(28 answers)
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 2 years ago.
I want the computer to randomly choose either 8 or 2.
I tried doing (Math.random()*8)+2, it chooses a random number BETWEEN 8 and 2 but I want the computer to randomly choose 8 or 2.
So I tried a different way, and I did this:
if(b==0){
var a = 2;
}
else{
var a = 8;
}
However, it is not working. Is there a more efficient and better way?
Thank you,
Paul
Something like this should do.
function randomSelection(values) {
return values[Math.floor(Math.random() * values.length)];
}
numbers = [2,8];
randomSelection(numbers);

Is possible to add NUMBER value return in Js? [duplicate]

This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 3 years ago.
I am using Javascript. In success function, I am getting some results and storing in var say "baldet" in JS-CODE.
example:
success: function (result) {
baldet = result;
console.log(baldet[0].cleavebalance);
if (openmon == 1){
$("#clvbalance").val(baldet[0].cleavebalance);
}
}
from baldet[0].cleavebalance I am getting 2. My condition is if openmon == 1 then I want to add number 1 in it.
NOTE: add means SUM, not CONCAT()
So that i could get, 2 (from "baldet[0].cleavebalance" + 1 (number i want to add)) i.e. 2 + 1 = 3.
and show in $("#clvbalance").val("3"); // value 3 is after getting SUM RESULT
Currently, I am getting "21" instead of "3"
I hope I made my query clear. Any help will be appreciated.
You need to parse from string to number like below
console.log("2" + 1); // dose not parse
console.log(parseInt("2") + 1)

Number value is getting changed in JS [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Is there any limitation for integer in javascript? [duplicate]
(2 answers)
Closed 6 years ago.
This is my post id:
var postId = 47213486358396931;
var postComments = "Comment";
adding in object like this
var param = {
"PostId" : postId,
"postComment":postComments
}
If I print the param I get like this. The last digit of number changed to 0. Why am getting like this.
{"PostId" : 47213486358396930, "postComment": "Comment"}

How to take a value from a link using jquery [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();

Categories

Resources