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>
Related
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>");
}
}
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 4 years ago.
Improve this question
How to loop on a number using jQuery from 1 to 10.
Let's say I have
var start=8;
var end=13;
Expected Output:
8 9 10 1 2 3
On reaching 10, the output series restarts from 1.
Try this
var start = 8, end = 13;
for(var i = start; i <= end; i++){
var num = i > 10 ? i % 10 : i;
console.log(num);
}
Try this
var start = 8;
var end = 13;
for(var i = start; i <= end; i++){
var result = i % 10;
if(result == 0){
result = 10;
}
console.log(result);
}
Try this it is quite straightforward.
var start = 8, end = 13;
for(var i = start; i <= end; i++){
console.log(i % 10 === 0 ? 10: i % 10);
}
var start = 5,
end = 13;
var i = start;
var j = start;
while (j >= start && j < end) {
console.log(i);
i = (i + 1) % 10;
j++;
}
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.
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);
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; }