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());
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 7 years ago.
Improve this question
If I have phone number ranges such as 5555555555-5599 5555550000-0003 5555550005-0007, my mission is to have it return all results without using a server. Is it possible for it to return without a server a result that would look like:
5555555555
5555555556
5555555557
/* etc */
My previous post about javascript has helped me up to this point but I wanted to rehaul the whole site.
Javascript dashes in phone number
If you could point me in the right direction, I would really appreciate it. I'm just having a mind block right now if this is even possible.
Given a single phone range in the form of "xxxxxxyyyy-zzzz", split the whole string on the dash and the first part of the string at the 6th index. This yields three strings "xxxxxx", "yyyy", and "zzzz". Using a for loop, you can create an array of phone numbers by concatenating the prefix "xxxxxx" onto the range "yyyy"-"zzzz":
// Get an array from a given range "xxxxxxyyyy-zzzz"
function fromRange(range) {
var phoneNumArray = [];
var prefix = range.substring(0,5);
var suffixRange = range.split("-");
for (var suffix = suffixRange[0].substring(4, -1);suffix < suffixRange[1];suffix++) {
phoneNumArray.push(prefix + suffix);
}
return phoneNumArray;
}
Try it in JSFiddle.
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 7 years ago.
Improve this question
I have to count length of a jsonArray but i'm able to do it. This is the example from start:
https://jsfiddle.net/vuqcopm7/13/
Summary, if you click over an item of the list, for example "asd1" it will create an input text for each time you click that item or the others. What i need is count how many times that item is clicked, or better, how many input that item creates. Is it possible? For example instead:
asd1 (numberForEachItemSelected)
if i create 2 input, because i tap over it 2 times, it will be:
asd1 (2)
everything in angularjs
Updated Fiddle
I changed the push pushItems function to increment a 'clicks' key I added to 'attributes', then I bound that to the view:
$scope.pushItems = function pushItems(attribute, items) {
items.clicks++
//do stuff..
}
If you can't modify your model to add the 'clicks' key server side, adding this if statement will either add to the clicks key, or set it if it doesn't exist:
$scope.pushItems = function pushItems(attribute, items) {
if (items.clicks) {
items.clicks++
} else {
items.clicks = 1
}
// do stuff
}
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;
}
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 8 years ago.
Improve this question
I have a left tree navigation with a search box on top.When the user enters some text in the Search box,the matching nodes of the tree navigation should get highlighted.
I want to do it using Java script,Can anyone point me to any such examples or documentation for this.
Thanks
Without any html etc can't really see what your setup is! But I assume you want something like the following:
http://jsfiddle.net/cwc66a3d/3/
Use indexOf to find any matching cases among the options, then using the position given, insert a span to contain the matching text with a yellow background to give the impression of highlighting.
The timeout is simply because of delays in event firing, sure to make sure it highlights in real time it is needed.
document.onkeydown = function () {
setTimeout(function () { //delay so can take the event fire into account
var list = document.getElementsByClassName('tree');
for (var i = 0; i < list.length; i++) {
list[i].innerHTML = list[i].innerText;
var pos = (list[i].innerHTML).indexOf(s.value);
if (s.value !== '' && pos != -1) {
var a = list[i].innerHTML.substring(0, pos) + '<span style="background: yellow;">' + list[i].innerHTML.substring(pos,1+ (list[i].innerHTML).indexOf(s.value.substr(s.value.length-1, 1))) + '</span>' + (list[i].innerHTML).substring(1+ (list[i].innerHTML).indexOf(s.value.substr(s.value.length-1, 1)), (list[i].innerHTML).length);
list[i].innerHTML = a;
}
}
}, 50);
};
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>");
}