Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
My developer is currently on annual leave and I need to work out how to make a change to some code.
The form input is a salary and I am able to output this salary using:
$('#demo').html($('#salary').val());
This displays within <div id="demo"></div>
I need to display the salary input as weekly (salary / 52).
Any assistance on how this can be achieved is appreciated.
I believed it to be:
$('#demo').html($('#salary'/52).val());
But this does not work.
In your example :
$('#demo').html($('#salary'/52).val());
you are trying to add the division to the jQuery selector. that won't work.
you will need to make sure the result of the selector is a number and then divide that by 52:
$('#demo').html(parseFloat($('#salary').val())/52);
$('#demo').html($('#salary').val()/52);
I recommend you read the jQuery Documentation. You first should get value with:
$('#salary').val(); //get value
After divide:
$('#salary').val() / 52
It is a simple programming logic. After you are able to set your value in any place, like:
$('#demo').html($('#salary').val()/52);
you are trying to add the division to the jQuery selector. that's Wrong.
$('#demo').html($('#salary'/52).val());
You need to make Sure first you get value of Salary then divide it into week. and it will return as string, so you have to make to Integer OR Float
Float :
$('#demo').html(parseFloat($('#salary').val())/52);
Integer :
$('#demo').html(parseInt($('#salary').val())/52);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
I have a JS function which checks if a user entered string is zero.
if (str.legth = 0) {
alert('Provide at least 1 character to create a folder.');
return;
}
But this seems to let a user pass a zero entered string.
Is there anything missing?
THanks
You've misspelled "length" and you need "==" instead of "=" to check for equailty rather than assigning a value.
If your variable is called str, then length is used to return a boolean value. You need to use either == or === for an actual comparison instead of an assignment. Additionally, a return isn't necessary.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
Hi im pretty sure what im about to post might not be enough info (if so please let me know what more is needed). I am using node js and having a really weird error. Below is the code and output.
if (currentPrice > variableData[i].stopLossHard) {
console.log('if')
console.log(currentPrice)
console.log('is more than')
console.log(variableData[i].stoplossHard)
}
Output:
if
92.7
is more than
93.62700000000001
This is consistently happening. I also made sure that both currentPrice and variableData[i].stopLossHard are numbers and not strings (I made sure in the code and in the output its the color of a number not a string)
Any ideas is highly appreciated.
The attribute you print is different than the one you check in the if statement:
(In the if stopLossHard has a capital L, stop-L-ossHard, what you print doesn't)
Try this:
if (currentPrice > variableData[i].stoplossHard) {
console.log('if')
console.log(currentPrice)
console.log('is more than')
console.log(variableData[i].stoplossHard)
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
Why does 0.toFixed(2) print 0 instead of 0.00?
The correct answer:
Use a variable (noted by Rajesh in the comments)!
var num = 0
var fixedStr = num.toFixed(2);
This just looks better, is easier to understand and also safer, as it will throw errors to you if any occur.
Some Warning
Please note that some interpreters (just like the chrome console) do throw an error if you do 0.toFixed(2), as it is no valid JS to them. If you use a variable or brackets around the 0, it will be okay for them.
Another way for doing it
Also noted in the comments (by 4castle):
You can also use the following:
0..toFixed(2)
As the first dot will be interpreted as a decimal point, this will be okay for the interpreter and be parsed into "0.00".
But please do not use this, use a variable. It just looks horrible and not everyone understands what this should do (or why it magically works).
If you store 0 in a var, or if you use (0), it give 0.00.
(0).toFixed(2)
"0.00"
var x = 0;
x.toFixed(2)
"0.00"
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I've made a super simple script to pop out some hourly rates from a pool of tips. Thing is, this one specific result always comes out wrong. What the heck is going on?
var tips = prompt('Enter final tips after payouts and cleaning');
//Hours worked for both positions
var tendHrsFirst = 11;
var tendHrsSecond = 10;
//Hourly Rate
var barThourly = ((tips/(tendHrsFirst+++tendHrsSecond)));
//This result here always comes out as if tendHrsFirst is 12 and not 11.
var barToneTotal = (tendHrsFirst * barThourly);
//This result is always correct
var barTtwoTotal = (tendHrsSecond * barThourly);
You are incrementing with tendHrsFirst++, so it actually is 12.
I guess those are actually two commands.
tendHrsFirst++ increments tendHrsFrist by 1. Afterwards, you add both numbers. Not sure why you think that's a good idea. Cleaning up your code should help avoiding such mistakes.
here
var barThourly = ((tips/(tendHrsFirst+++tendHrsSecond)));
you are using +++ that means postfix increase of tendHrsFirst and added to tendHrsSecond
or maybe
prefix increase of tendHrsSecond added to tendHrsFirst
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 have the following element on my page:
<div id="price">$8.00</div>
I want to be able to assign the value 8.00 to a JavaScript variable so that I can perform math on it (specifically 8.00 * .2), and then want to take the new number and place it next to the original, while applying a line-through on the original number ONLY. So the outcome would look something like this:
$8.00 $1.60 (where the "$8.00" has a text-decoration:line-through applied to it.)
I have been able to figure out the math and resetting the text, but I need help figuring out how to take something that's displayed, and make assign its value to a variable. I am using jQuery. Any help is appreciated.
$('#price').text() will get you the text. From there, you can use parseFloat() to get the number. However, you have a $ in there, so you will need substring as well.
var price = parseFloat($('#price').text().trim().substr(1));
You can either use regex to find just the number and use that, or you can get all the information and parseFloat, which will return just the number... Or, if you can modify your html, you can do
<div id="price">$<span id="actual_price">8.00</span></div>
parseFloat($('#price').text()) will give you the number as a floating point number.
however if you want to get a variable you must use var to assign one.
Also, you will need to use substring to just get the 8.00 or the variable will be NaN.
HTML
<div id="price">$8.00</div>
jQuery
var price = $('#price').text().substring(1);
newPrice = price * 0.2;
newText = '<span>$' + price + '</span> $' + newPrice;
$('#price').html(newText);
CSS
#price span { text-decoration: line-through; }