I've tried hard, but I just can't figure it out.
I want to output numbers, but only one character of the number at time. I need to create something like this:
This should be created within a for-loop:
http://jsfiddle.net/jv7H8/
But as you can see there is more than one character in a cell when number > 10.
The desired result should be for example:
1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4....
Any suggestions? :)
First concatenate the numbers into a string:
var s = '';
for (var i = 1; i <= 42; i++) s += i.toString();
Then loop the characters in the string:
for (var i = 0; i < s.length; i++) {
// output s[i]
}
Here is your jsfiddle updated with my approach: http://jsfiddle.net/jv7H8/2/
The pertinent aspects that I changed was adding a for loop that processed through the length of the number you were going to output:
var str = number.toString(); // the current number as you're looping through
for (var k = 0; k < str.length; k++) {
var oneLetterAtATime = str.charAt(k); // get the digits one at a time
output += "<td>" + oneLetterAtATime + "</td>";
}
number++;
Edit: If you need there to only be nineteen columns, then you'll need to update your column counter for every instance where you are displaying another <td> but not looping back around to increment your column counter. I.e.,
if (k > 0) {
j++;
}
Here is an updated version displaying how this would work: http://jsfiddle.net/jv7H8/21/
Notably, there isn't a very good way to not go past the 19th column when you are in the middle of displaying a number in the 19th column that has more than one digit.
Take all the characters in a string like this:
var t = '';
var limit=100;
for (var j = 1; j<= limit; j++) t += j.toString();
var output='';
for (var j = 0; j < t.length; j++) {
output=output+','+t[i];
}
alert(output);
Please check your updated fiddle here
This will server your purpose.
Following is the code, most of it is your code only:
rows = 3;
columns = 19;
number = 1;
var str = "";
output = '<table style="width:100%;">';
for(var i = 0; i < rows; i++) {
output += "<tr>";
for(var j = 0; j < columns; j++) {
output += "<td>" + number + "</td>";
str +=number;
number++;
}
output += "</tr>";
}
output += "</table>";
$("#game").html(output);
var strvalue="";
$.each(str, function(e,v){
if (e > 0){
strvalue = strvalue + ", "+ v;
}
else{
strvalue += v;
}
});
alert(strvalue);
Related
I need to print a number series like below in javascript
123567101112161718232425
After 3 numbers next will be empty. that will increase as 1 number missed, next 2 number missed like that.
Can anyone help me how to do that..
Something like this might work I believe:
Idea: after every 3 numbers, increase the jump count. Before increasing, just add it to the current Number.
var retStr = '';
var jump = 1;
var currNum = 1;
for(var i=0;i<10;i++){
for(var j=0;j<3;j++){
retStr = retStr + (currNum++);
}
currNum += jump++;
}
console.log(retStr);
Output: "123567101112161718232425313233404142505152616263737475"
var serie = "";
var skip = 0;
for (var i=1; i<100; i++)
{
serie = serie + i + (i+1) + (i+2)
i= i+2
skip = skip +1;
i= i + skip;
}
console.log(serie);
var count = 1 ;
var j=1;
for(var i=0;i<5;i++){
for(j=count; j<(count+3); j++){
document.write(j);
}
count = j+=i+1;
}
function Print(N) {
var arr = [];
var k =0;
var p =0 ;
for (var i = 1; i <= N; i++) {
arr.push(i + k)
if (arr.length % 4 == 0) {
k= k+p ;
p++;
k++;
}
}
return console.log(arr);
}
Print(20)
This question already has answers here:
'Length' Property Undefined while iterating Array
(3 answers)
Closed 6 years ago.
class CustomTable
{
constructor(div_id, headings) {
this.div = div_id;
this.header_titles = headings;
this.item_list = new Array();
var _this = this;
this.add_item = function(items)
{
_this.item_list.push(items);
console.log(_this.item_list);
}
this.remove_item = function(item_index)
{
_this.item_list.splice(item_index, 1);
console.log(_this.item_list);
}
this.drawTable = function()
{
var t = "<table class='table' style='width:100%'>";
t += "<thead>";
t += " <tr>";
t += " <th>#</th>";
for (var i = 0; i < _this.header_titles.length; i++)
{ t += "<th>" + _this.header_titles[i] + "</th>"; }
t += " <th>Add</th>";
t += " </tr>";
t += "</thead>";
t += "<tbody>";
for (var i = 0; i < _this.item_list.length; i++)
{
t += "<tr>";
t += "<td>" + i + "</td>";
console.log(i);
var subitem_count = _this.item_list[i].length;
// ^^^^^^^^^^^^^^^^^^
// This errors out: TypeError undefined
for (var j = 0; j < subitem_count; i++)
{
t += "<td>" + _this.item_list[i][j] + "</td>"
}
t += "</tr>"
}
t += "</tbody>";
t += "</table>";
document.getElementById(_this.div).innerHTML = t;
}
}
}
var ct = new CustomTable("server_list",["Server Name","IP Address", "RAM in GB"]);
ct.add_item(["QMM-TRGEXCH01","192.168.0.225","2GB"]);
ct.add_item(["QMM-SRCEXCH01","192.168.0.226","2GB"]);
ct.add_item(["QMM-TRGAGENT01","192.168.0.227","2GB"]);
ct.add_item(["QMM-SRCAGENT01","192.168.0.228","2GB"]);
ct.add_item(["QMM-MIGCONSOLE","192.168.0.229","2GB"]);
ct.drawTable();
Please view this JSFiddle
I have searched everywhere and can't figure out why Javascript keeps erroring out. The variable is in scope and I have checked it using
_this.item_list[i].constructor === Array
and it is an Array.
I get this error at first iteration.
console.log(i); // i = 0 at error
So its not that the code is iterating out of bounds. That might be an issue with the code as well but there is something else wrong. Please look at the fiddle, I have updated it and remove the = from all for loops but I still get the same error.
It is because you are trying to invoke a method on element at unavailable index.
for (var i = 0; i <= _this.item_list.length
is supposed to be
for (var i = 0; i < _this.item_list.length
Array out of bounds issue - You are trying to access the element at index 6 which is undefined
As per your logic, if they are 5 elements in _this.item_list you are iterating elements at index 0, 1, 2, 3, 4, 5 but you should only be iterating upto 4 as the index starts at 0 and not 1
You will have to replace all instances of <= in your for-loop to <
Also your inner loop has a bug.
for (var j = 0; j < subitem_count; i++)
supposed to be
for (var j = 0; j < subitem_count; j++)
You should be incrementing j and not i in the inner loop.
Check Fiddle
I am trying to print this pattern using javascript
10
10 20
10 20 30
10 20 30 40
10 20 30 40 50
So far I've tried a for loop
for (i=10; i<=50; i+=10){
document.write(i+' \b');
}
How should I enhance the code?
var i, p = [];
for (i=10; i<=50; i+=10){
p.push(i);
document.write(p.join(' ') + '<br>');
}
Edit for #macmee
var i, p = [];
for (i=1; i<=5; i++){
p.push(i*10);
document.write(p.join(' ') + '<br>');
}
This leaves no trailing spaces at the end of any lines.
var p = "";
for (var i = 10; i <= 50; i += 10){
p += ' ' + i;
document.write(p.trim() + '<br>');
}
Ignoring the issues with using document.write for a task like this:
var prevEntry = "";
for (i=10; i<=50; i+=10){
prevEntry += i+' \b';
document.write(prevEntry + '<br>');
}
This seems like a homework question because document.write should really never be used, but anyway:
// store answer here
var str = '';
// makes sure 1 number is printed on line 1, 2 on line 2, etc
for(var i = 0; i < 5; i++) {
// makes sure 1 number is printed on row 1, 2 on row 2, etc
for(var j = 0; j <= i; j++) {
str += (j+1)*10 + ' ';
}
str += '<br>';
}
// write it to the document (please consider using jquery instead)
document.write(str);
I'm trying to create a simple algorithm that builds an array with a dynamic length.
Then, it will, one by one, replace an item, and then two, then three and so on until the only items left are the first and last.
like this:
12345
1*345 // it never touches the first
12*45
123*5 // it doesn't ever touch the last item
1**45
12**5
1***5 // done, nowhere else to go
I put together a simple demo to show what I'm trying to do.
var length = 6,
array = [],
log = document.getElementById("log"),
edited,
j,
i;
for (i = 1; i <= length; i++) {
array.push(i);
}
log.innerHTML += array.join(" ") + "<br><br>";
for (i = 1; i < (length - 1); i++) {
edited = array.concat();
for (j = i; j < (length - 1); j++) {
edited[j] = "*";
log.innerHTML += edited.join(" ") + "<br>";
}
log.innerHTML += "<br>";
}
Fiddle
It works fine, the only problem is it's out of order.
Right now it seems to only iterate by number of asterisks, then by index. I need it to do the opposite.
// it does this
12345
1*345
1**45
1***5
12*45
12**5
123*5 // out of order
If someone could help that would be great because I am really at a loss!
This should get it done.
var a = 6, // array length
b = [], // array
log = document.getElementById("log"),
c,
d,
e;
for (c = 1; c <= a; c++) {
b.push(c);
}
log.innerHTML += b.join(" ") + "<br><br>";
//the size of the asterisk chunk
for(i = 1; i < b.length - 1; i ++)
{
//position to start asterisk chunk
for(j = 1; j < b.length - i; j ++)
{
var tempArr = b.concat();
//the position inside of the asterisk chunk
for(k = 0; k < i; k ++)
{
tempArr[k + j] = "*";
}
log.innerHTML += tempArr.join(" ") + "<br>";
}
}
JSFiddle
This seems to work well:
str = "1234567"
len = str.length;
for(var stars = 1; stars < len - 1; stars++) {
for(var pos = 1; pos < len - stars; pos++) {
var s = str.substr(0, pos)
+ new Array(stars + 1).join("*")
+ str.substr(pos + stars);
document.write(s + "<br>");
}
}
Let's assume I have two variables. One an array of numbers and the other the number 3. The goal is to iterate through the array of numbers and figure out which pair of numbers can be used to equal the number 3 either by being added together or subtracted.
var numbers = [-1, -1, 4, 2, 3, 5, 0]
var target = 3
for(i = 0; i < numbers.length; i++) {
}
I understand the for loop is going to go through the array of numbers but once I do that I don't understand how I can check every pair and see if they add or subtract to hit the value of 3. Is there a JavaScript method that can help?
Not sure if I understood correctly, but maybe something like this?
var numbers = [-1, -1, 4, 2, 3, 5, 0];
var target = 3;
var pairs = [];
for (i = 0; i < numbers.length; i++) {
for (j = 0; j < numbers.length; j++) {
if (j != i) {
if ((numbers[i] + numbers[j]) == target) {
pairs.push([numbers[i], numbers[j]]);
document.write(numbers[i] + " + " + numbers[j] + " = " + target + "<br>");
}
}
}
}
Basically you go through each number in the array, then loop again through all the numbers and check if their sum equals to the target.
You can test it here.
I don't think there is a JavaScript method for this, but this should work:
for(i = 0; i < numbers.length; i++) {
// calculate the difference
var diff = target - numbers[i];
// now: numbers[i] + diff === target
// do whatever you want with diff
}
for (var i = 0; i < numbers.length-1; i++){
for (var j = i+1; j < numbers.length; j++){
if(numbers[i] + numbers[j] == target || Math.abs(numbers[i] - numbers[j]) == target){
console.log(numbers[i]+" , "+numbers[j]); //Do whatever you want
}
}
}