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'm beginner in Emberjs,
I want to calculate the total amount in the added item list.
For example:
(03) items added in list
------------------------
Item-1 3,000.00
Item-2 4,000.00
Item-3 3,000.00
-------------------------
Total 10,000.00 Print
-------------------------
How to create the view and controller in emberjs for calculate the total amount?
How to create the PDF file and print the same output as PDF format?
Thanks advance. :)
The aggregation methods in Ember.Enumerables are handy for this type of thing. I've created a jsBin demonstrating how to use reduce() in this case. You can create a computed property for the total on your controller that sums the values of each record in your model. Note that passing 0 as the second parameter (initialValue) will seed reduce() with an integer and help you avoid the string concatenation issues you were seeing.
total: function () {
return this.get('model').reduce(function (previousValue, item, index, enumerable) {
return previousValue + item.value;
}, 0);
}.property('model.#each')
(Your second question is unrelated, but you might want to take a look at jsPDF.)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 15 days ago.
Improve this question
I have other ways of doing the same thing .. So I am not looking for a different solution to this problem ... I am looking for an explanation as to why if I have defined a integer, it still concatenates with .map as if it were a string.
I have a basic set of data retrieved from an API:
"data":["8","8","12","1","7","4","2"]
If I map it using
let count = response.data.metrics.data.map((item) => + parseInt(item));
I am having a hard time understanding why it's treating this as a string returning
88121743
When I feel like because I am parsing it as an integer it should add and come out with 42.
Is this just an issue just using .map? Can shortcut math functions be used here?
Here is my Reproducible Example
Your current approach using Array#map creates a new array with each element converted to a number. React renders this array as multiple text nodes, so it looks like a single string.
To sum the values, use Array#reduce with parseFloat/parseInt, the Number function, or the unary plus operator to convert each string to a number.
const visitCount = data.reduce((a,b) => a + parseFloat(b), 0);
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;
}
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'
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
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; }