Creating a table with a fixed number of rows [closed] - javascript

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 9 years ago.
Improve this question
Using Javascript, Html and BFO (Big Faceless Org) PDF Generator, I need to create a table with fixed number of rows for every page in a sales order. Basically, the table should consist of fixed 10 rows, however rows populated with information may only be 1 or 2 rows. The other rows will be empty.
Anyone can help?
Currently all I have is:
for(I=0;i<=salesitem.length;i++){
document.write('<tr><td>salesitem[I]</td></tr>');
}
That generates one td row for every sales item. However, I want a fixed 10 rows in the table.

There are several issues with the tiny piece of code you've supplied...
javascript is case sensitive, therefore I and i are different variables
looping from 0 to i<=salesitems.length will end in an error, because if length is 10 and you use salesitem[10] it will fail (arrays are 0-based, and therefore an array with length of 10 has items 0 to 9)
I believe you think that salesitem[I] will process like PHP, it won't. PHP allows you to use echo "print this $varName", javascript simply doesn't allow that.
Try something along these lines...
for (i = 0; i < salesitem.length; i++) {
document.write('<tr><td>' + salesitem[i] + '</td></tr>');
}
for (i = salesitem.length; i < 10; i++) {
document.write('<tr><td></td></tr>');
}
The first loop will display everything in your array (including any entries over 10)
The second loop will display lines to complete the table (if there are less than 10 entries)

Try this.
for (var i=0;i<10;i++) {
document.write("<tr>");
//This will depend entirely upon what conditions have to be met
//in order for you to display the content of each row
if(variable == 'condition') {
document.write("<td>content</td>");
}
document.write("</tr>");
}

Related

How to get radar chart? [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 1 year ago.
Improve this question
I wanted to get a radar chart using the following query. But the graph is not displayed. What is the problem?
str= "{type:'column',showInLegend: true,name:'"+userdet.getCompname()+"'"
+ ",dataPoints:[{y:"+aa+",label:'self'},{y:"+r+",label:'average'},{y:"+bb+",label:'superior'}]},";
sb.append(str);
I am getting data from the the following table. "aa" is self marks, "bb" is superior marks and "r" representing the average marks. "userdet.getCompname()" will retrieve the competency name from the table report. table report
The LIMIT keyword isn't allowed in an INSERT statement. It limits the number of rows returned by a query.
Your insert query is only ever going to insert 1 or 0 rows (since you only have one set of values but might hit an error if you duplicate a unique key).
If you want to limit the number of rows then you can do two queries:
Count the number of rows (with a SELECT)
Insert the new row (if the count isn't too high)
You should wrap the two queries in a transaction to avoid race conditions.
A cursory Google suggests that there might be database specific approaches to doing an insert if with a row count, but they will depend on whatever database server you are using (and didn't mention in the question).
The Limit 5 is for retrieving the data i.e. it will return 5 rows. I think you dont want to add any rows where there is already 5 rows inserted. You can check first the count of rows in table and after that you can add when count is less than 5.
SELECT count(*) from question;

I need to programatically create several variables in javascript? [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 6 years ago.
Improve this question
I asked this question but it was marked as duplicate - it's not.
I need to programmatically create X number of variables (the number will be the value of someArray.length - which is also something created programmatically and thus unknown to me).
Is there any way to say "there's 8 items in this set, so I need 8 variables, and I need to be able to call on them/manipulate them later?" Hopefully I'm making sense.
My core problem: I programmatically generate a list of links. Can be any number of links. I need to be able to validate that the user clicks each of these links before advancing.
I programmatically generate a list of links. Can be any number of links. I need to be able to validate that the user clicks each of these links before advancing.
You do not need to create an unknown number of variables to solve your problem (how would you name them?). As stated by other commenters, you have a list of links, and you need to keep track of which ones have been clicked.
There are numerous ways to solve this problem in JavaScript. Below is an example of a straightforward approach which should be easy to follow. The approach is simply to use another array, linksClicked, to keep track of which links have been clicked. To validate, count the number of links that have been clicked and compare to the total number of links.
var arrayOfLinks = [
'http://www.stackoverflow.com',
'http://www.arstechnica.com'
];
var linksClicked = [];
function clickLink(url){
//check if link is in arrayOfLinks
for(var i = 0; i < arrayOfLinks.length; i++){
//if link is in arrayOfLinks, mark it as clicked
if(arrayOfLinks[i] === url){
linksClicked[i] = true;
}
}
}
function checkLinksClicked(){
//count number of links that have been clicked
var linkSum = 0;
for(var i = 0; i < linksClicked.length; i++){
if(linksClicked[i]){
linkSum++;
}
}
return linkSum;
}
console.log(checkLinksClicked());
clickLink('http://www.stackoverflow.com');
console.log(checkLinksClicked());
clickLink('http://www.stackoverflow.com');
console.log(checkLinksClicked());
clickLink('http://www.arstechnica.com');
console.log(checkLinksClicked());

What is good naming form for variables in loops JS? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Hi so as I mentioned in the title I have a bunch of loops within loops in my code and most of them use a variable to loop through stuff. I call my variables ia, then ib then ic and so on. What is good form for the naming of these variables?
Here is some code that might help make sense of what I am saying
for (var ic = 0; ic <= currState.length; ic++) { //loop the columns and check if there is a
if (currState[ic] == 0) {
for (var id = 1; id <= currState.length; id++) { //loop the rows of a column
tryPos = [id, ic + 1]; //id -> row | ic -> column
if (checkClash(currState, tryPos) == false) {
currState[ic] = id
break;
}
}
}
}
The name of your variables requires good descriptive skills and a shared cultural background.
In this case you should use row and col but don't forget the scope of your variables.
I would like recommend you read the Book: Clean Code.
Especially you can review some of the proposed rules on this post (http://www.itiseezee.com/?p=83).
looping over a table or grid, row and column are perfectly fine variable names.
for more generic nested loops i, j, and k (in that order) are typical counter variables even though they are less aptly named

how to display all elements of an array 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
I wish to print elements of the array below
var array = [1,2,3,4,5];
However, only 3 digits must be printed at a time, e.g,
step 1: should print 1,2,3
step 2: should print 2,3,4
step 3: should print 3,4,5
step 4: should print 1,2,3
step 5: should print 2,3,4
and the loop continues .....
I wish to display the thumbnails of this slider in a manner that they will fit a small screen.
http://www.tympanus.net/Tutorials/FullscreenSlideshowAudio
That means only a few thumbnails must appear at a time, while the rest are invisible for a short while.
Thanks..
As you wanted to display 3 elements from the array each time and when the last element is displayed you want to repeat the process again you are suppose to use setInterval() to call the function after a specified time continuously and reset the index to 0 immediately after the last element is displayed. Else increment the initial or counter value. Try this way,
var array = [1,2,3,4,5];
var initVal = 0;
setInterval(function(){
console.log(array[initVal] + "," + array[initVal+1] + "," + array[initVal+2]);
initVal = initVal == 2 ? 0 : initVal+1;
}, 500);
jsFiddle

JavaScript average calculator [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 am wondering how i would make a function that will record the 10 most recent Date.now commands and then turn them into an average. I then want to put it into a side bar and make it sort of like a scoreboard. http://jsfiddle.net/eh5dxyp4/ . Thanks in advance
clickedTime=Date.now();
reactionTime=(clickedTime-createdTime)/1000;
document.getElementById("time").innerHTML=reactionTime;
this.style.display="none";
makeBox();
}
makeBox();`
You've shown quite a bit of code that doesn't seem relevant to the actual maths part of your question. I'm going to provide one way to do this part:
record the 10 most recent Date.now commands and then turn them into an average
Create an array:
var recent = [];
And a function that adds a value to that array but also ensures there will only be at most ten items in it:
var recentLimit = 10;
function addRecentItem(item) {
recent.push(item); // add to end of array
if (recent.length > recentLimit)
recent.shift(); // remove from start of array
}
Then you just need a function to calculate the average:
function getRecentAverage() {
var i,
sum = 0;
for (i = 0; i < recent.length; i++)
sum += recent[i];
return sum / recent.length;
}
So then wherever in your code you produce one of the Date.now objects you can simply add it to the recent list:
addRecentItem(yourValueHere);
And then get the current average:
console.log( getRecentAverage() );
As far as your scoreboard concept goes, you just need a function that loops through whatever is in the recent array and creates appropriate html elements (li elements, for example).
Add var reactionTimes=[]; somewhere outside the function that calulates it then use
var reactionTime = (clickedTime-createdTime)/1000;
reactionTimes.push(reactionTime);
document.getElementById("time").innerHTML=reactionTime;
if (reactionTimes.length==10) {
var average = reactionTimes.reduce(function(sum, a) { return sum + a },0)/(reactionTimes.length!=0?reactionTimes.length:1);
reactionTimes.pop(); // make ready for a new 10th value
document.getElementById("average").innerHTML=average;
}

Categories