Javascript - Sum table column with decimals [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a script which calculating all column values in table, but now the script gets only full number, not calculating decimal, so if I have:
column1 ------- column2
some text ----- 10.2
other text ------ 3.4
textt ------------- 20.9
I am getting just 33 not 34.5
My code:
var tds = document.getElementById('vinyle').getElementsByTagName('td');
var sum = 0;
for (var i = 0; i < tds.length; i ++) {
if (tds[i].className == 'count-me') {
sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
}
document.getElementById('vinyle').innerHTML += '<tr class="sum"><td>' + sum + '</td></tr>';
How to edit this to start calculating with decimals?

Use parseFloat()
var tds = document.getElementById('vinyle').getElementsByTagName('td');
var sum = 0;
for(var i = 0; i < tds.length; i ++) {
if(tds[i].className == 'count-me') {
sum += isNaN(tds[i].innerHTML) ? 0 : parseFloat(tds[i].innerHTML);
}
}
document.getElementById('vinyle').innerHTML += '<tr class="sum"><td>' + sum.toFixed(2) + '</td></tr>';
var tds= ['36.2','56.2'];
var sum = 0;
for(var i = 0; i < tds.length; i ++) {
sum += isNaN(tds[i]) ? 0 : parseFloat(tds[i]);
}
document.getElementById('vinyle').innerHTML += '<tr class="sum"><td>' + sum.toFixed(2) + '</td></tr>';
<div id="vinyle"></div>

Use parseFloat instead of parseInt.

Related

How do I write a function to multiple numbers from 1-10? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
var a = 7;
var b;
for (var i = 1; i <= 10 ; i++) {
b = a * i;
document.write(" " +a+ "*", + b + "<br>");
}
This works however it has to be a loop, not code for one specific number so I need to write it so it goes for all numbers 1 through 10.
I'm using http://js.do which we have to use.
You need to have two loops - one nested inside the other - both going from 1 to 10.
Something like:
for (let i = 1; i <= 10; i++) {
for (let j = 1; j <= 10; j++) {
let s = i * j;
document.write(i + "*" + j + "=" + s + "<br>");
}
}
Or, to allow the user to enter a number of their choice:
let a = prompt("Enter a number: ");
if (a) {
for (let i = 1; i <= 10; i++) {
let b = i * a;
document.write(a + "*" + i + "=" + b + "<br>");
}
}
var a;
var b;
for (var a = 1; a <= 10 ; a++) { // loop the a number 1 to 10
for (var i = 1; i <= 10 ; i++) {
b = a * i;
document.write(" " +a+ "*", + b + "<br>");
}
}

Javascript row number using for loop? [closed]

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 2 years ago.
Improve this question
Can anyone help me with this Javascript problem? The code I've write is not quite right.
function tes() {
var c = document.getElementById('Text');
var a = document.getElementById('a').value;
var txt = '';
for (i = 1; i <= a; i++) {
for (j = 1; j <= a; j++) {
if (j == i) {
txt += i.toString();
} else {
txt += i.toString();
}
}
txt += '<br/>';
}
c.innerHTML = txt;
}
<input id="a">
<button onclick="tes()">Input</button>
<div id="Text"></div>
Javasript Problem
Expected output:
1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5
Since your problem required an input, you would be running the code when input is received. The only thing you got incorrect was really the if statement logic. rowAmm is a variable holding for the amount of 1s in a row, if j was less than that, add another 1, otherwise add i which was the row number.
You won't have to do a .toString() function either if you're adding it to a variable which is already declared a string var txt = ''. Just like 5 + "5" will become 55 while 5 + 5 will be 10
var a = document.getElementById("a");
var textBox = document.getElementById("Text");
function makeSquare() {
var b = a.value;
var txt = '';
for (i = 1; i <= b; i++) {
var rowAmm = b - i;
for (j = 1; j <= b; j++) {
if (j <= rowAmm) {
txt += "1";
} else {
txt += i;
}
}
txt += '<br/>';
}
textBox.innerHTML = txt;
}
<input id="a">
<button onclick="makeSquare()">Input</button>
<div id="Text"></div>

JavaScript Odd Number Sorter Coding Challenge [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Ok so I'm working on learning JavaScript and I came across a Coding Challenge question which I can not solve. It gives me the outline/skeleton of the format I should use and wants me to find the odd numbers from 1-5000.
Starting with the basic function given below, write a function called sumOddNumbers that will print to the console and return the sum of all the odd numbers from 1 to 5000. Consider using a loop, and don't forget to call the function afterwards!
~ Format ~
function sumOddNumbers() {
var sum = 0;
// Your code here
console.log(sum);
return sum;
}
There is no need for a loop:
console.log(5000**2/4);
If 5000 is a dynamic input to your function, then the formula is as follows:
function sumOddNumbers(n) {
return (n + n%2)**2/4;
}
console.log(sumOddNumbers(5000));
If really it has to be done with a loop, and according to the template (which is not very nice BTW):
function sumOddNumbers() {
var sum = 0;
for (let i = 1; i <= 5000; i+=2) {
sum += i;
}
console.log(sum);
return sum;
}
sumOddNumbers();
let sum = 0;
for (let i = 1; i < 5000; i += 2) {
sum += i;
}
let sum = 0;
for (let i = 0; i < 5000; i++) {
if (i % 2 !== 0) {
sum += i;
}
}
function sumOddNumbers(max) {
let sum = 0;
for(let i = 1; i <= max; i+=2){
sum += i;
}
console.log(sum);
return sum;
}
sumOddNumbers(5000);

How to write this piece of code in jQuery? [closed]

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 8 years ago.
Improve this question
I want to implement this javascript code in jquery, please!
var html = '';
for (i = 0; i < 2; i++) {
for (var j = 0; j < 7; j++) {
var num = Math.floor(Math.random()*39)+1;
html += num + ' ';
}
html += '<br>';
}
document.getElementById('rows').innerHTML = html;
There is nothing wrong with the code you posted and there is not much difference between the straight javascript version and a version written in jQuery. The only different is the last line, where you select the rows object. In jQuery you get the object using $('#rows').
Here you go:
var html = '';
for (i = 0; i < 2; i++) {
for (var j = 0; j < 7; j++) {
var num = Math.floor(Math.random()*39)+1;
html += num + ' ';
}
html += '<br>';
}
$('#rows').html(html);

How to add a variable in javascript? [closed]

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 8 years ago.
Improve this question
var count=0 ;
for(var x=0; x<data_len; x++)
{
count = count + num_arr[x];
}
// alert(count);
If count = 352 I want to add 3+5+2 which is 10 and then 1+0 which is 1.
function sumParts(x) {
var sumX = 0;
var strX = x.toString();
var arrX = strX.split("");
for (a = 0; a < arrX.length; a++) {
sumX += parseInt(arrX[a], 10);
};
return sumX;
}
y = sumParts(count);
z = sumParts(y);
// y = 10; (3 + 5 + 2)
// z = 1; (1 + 0)
And, I believe (untested), if the return was changed to return sumParts(sumX), it would continue until it was a single digit integer.
You have an array of strings, not numbers. You can convert them to numbers with:
count = count + +num_arr[x];
The second + is the unary plus operator, and will cast num_arr[x] to a number.
If your numbers are all integers, you can use:
count = count + parseInt(num_arr[x], 10);
or (if you have floats):
count = count + parseFloat(num_arr[x]);
Convert count into a string :
var count = 352;
count += ''; // makes a string : "352"
while (count.length > 1) {
count = Function('return ' + count.split('').join('+') + ';')() + '';
}
This part :
Function('return ' + count.split('').join('+') + ';')
Gives successively :
function () { return 3+5+2; }
function () { return 1+0; }

Categories