Get line from file - NodeJS function [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How to write a function that returns a line from file with nodeJS? The program runs in the loop, and each time you call the function should return a new string. And if the file ends begins again, with the first line.
This function takes randomly, how to do consistently?
var fs = require('fs');
// Get random line from file
function getRandSocks() {
var socks = fs.readFileSync('socks.txt').toString().split("\n");
var randsock = socks[Math.floor(Math.random()*socks.length)];
return randsock.split(':');
}
function loop() {
// ...
var socks = getRandSocks();
console.log('host: '+socks[0]+'port :'+socks[1]);
//...
loop();
}

Perhaps this would help. This gets the next line and wraps back to the start. It is based on the given getRandSocks function with minimal change.
var current_line = 0;
function getNextLines() {
var socks = fs.readFileSync('socks.txt').toString().split("\n");
var randsock = socks[current_line % socks.length];
current_line = (current_line + 1) % socks.length
return randsock.split(':');
}

Related

I can't change data of var element with JavaScript in Html [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Please help me for change data of var element with JavaScript. I can't. I need If check.innerText changed to need change phone number.
function call(){
var check = document.getElementById("locinfo").innerText;
var tellnum = "tel:+998951441000";
if (check=="Катартал") {
tellnum = "tel:+998998047911";
}
else if (check=="Куйлик") {
tellnum = "tel:+998951440010";
} }
window.location.href = tellnum;
}
It's seem like you have one spare },
this should work.
function call(){
var check = document.getElementById("locinfo").innerText;
var tellnum = "tel:+998951441000";
if (check=="Катартал") {
tellnum = "tel:+998998047911";
}
else if (check=="Куйлик") {
tellnum = "tel:+998951440010";
}
window.location.href = tellnum;
}

How to check the array is of how many dimension in javascript? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
For example I have an array as follows & expected output is given.
In javascript how can we determine dynamically how many levels are there in my array ary.
var ary = ["a","b",["c","d"],"e",["f","g",["h","i"],"j"]];
Output: 3
var ary = ["a","b",["c","d"],"e",["f","g","i"]];
Output: 2
var ary = ["a","b",["c",["d"]],"e",[["f","g",["i","j"]],"k"]];
Output: 4
Here is a reccursive function that will traverse through the depths of the array and track the maximum of it. Note that the tracking is done via properties attach to the function itself.
var ary1 = ["a","b",["c",["d"]],"e",[["f","g",["i","j"]],"k"]];
function getDimension(arr, start) {
//Attach depth tracking properties to the function
if (start){
getDimension.depth = 0;
getDimension.maxDepth = 0;
}
//Track max depth
getDimension.depth++
if (getDimension.depth > getDimension.maxDepth)
getDimension.maxDepth++;
//Manage recursion
for (let element of arr)
if (element instanceof Array)
getDimension(element);
getDimension.depth--;
//In first level at this point
if (getDimension.depth === 0)
return getDimension.maxDepth;
}
let d = getDimension(ary1, true);
console.log(d);

cannot access the data of the first API in second API function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
<script>
/* 1st API*/
$.getJSON("http://cricapi.com/api/matches/?apikey=6aP8B4MImxSNxI6fevBmC2ZshO42",function(data){
var len = data.matches.length;
for(i=0;i<len;i++){
id = data.matches[i].unique_id;
ms = data.matches[i].matchStarted;
x = data.matches[i].date;
team_1 = data.matches[i]['team-1']
/* 2nd API*/
$.getJSON("https://cricapi.com/api/cricketScore/?apikey=6aP8B4MImxSNxI6fevBmC2ZshO42&unique_id="+id,function(data1){
console.log(team_1);
});
}
});
why i cannot get the team_1 of each match in second getJSON(). It is giving the output of same team name multiple times please help me to get the team names of each match. Appreciate any help!!
This "bug" is caused by hoisting.
Index i is stored as var, and it isn't in local for loop scope, so, when the index has gained its final value, the same value is used.
You can use let keyword:
for(let i=0;i<len;i++){ ..
so the variable will be loop scoped
Also Check out this link
function getScore(id, callback) {
$.getJSON("https://cricapi.com/api/cricketScore/?apikey=6aP8B4MImxSNxI6fevBmC2ZshO42&unique_id="+id, function(data1) {
callback(data1);
});
}
$( document ).ready(function() {
$.getJSON("http://cricapi.com/api/matches/?apikey=6aP8B4MImxSNxI6fevBmC2ZshO42",function(data){
var len = data.matches.length;
for(i=0;i<len;i++){
id = data.matches[i].unique_id;
ms = data.matches[i].matchStarted;
x = data.matches[i].date;
team_1 = data.matches[i]['team-1']
/* 2nd API*/
getScore(id, function(data1) {
console.log(data1);
});
}
});
});

Userscript Interval [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
var x = document.getElementsByClassName("farm_icon_c");
for(var i=0;i<x.length;i++){
x[i].click();
setInterval(function () {}, 1000);
};
var w=frames.main||self, d=w.document, b=d.links[0].href.match(/(village=)(\d+)/);
w.location.href=w.location.href.replace(/&*village=[pnj]?\d+&*/g, '&').replace(/&+$/,"")+'&'+b[1]+'n'+b[2];
void(0);
see this script works but waaaaay too fast.. This interval function isnt working
I think you want something like
var x = document.getElementsByClassName("farm_icon_c"),
(function f(i) {
if(i < x.length) {
x[i].click();
setTimeout(function() { f(i+1); }, 1e3); // Run next "iteration" in 1 second
} else {
// This code will run at "the end of the loop"
}
})(0); // Run first "iteration" immediately

JS loop producing no result [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
Trying to create an array with a loop and then sum all the contents of the array and put the result as the contents of a DIV but when I run the fiddle I get no result and jshint says everything is well formed.
var begin = 500000;
var stop = 999999;
var arrInt = [];
// Create an array worth summing
var CreateArray = function (begin,stop) {
while(begin < stop +1){
arrInt.push(begin++);
}
};
// Sum all ints in an array
var IntSum = function (ManyInts) {
var i = arr.length; while (i--) {
return;
}
};
var example = document.getElementById("example").innerHTML;
example=IntSum(arrInt);
<div id="example"></div>
http://jsfiddle.net/7b8rqme5/
At no point do you call CreateArray. You call your other function, IntSum, which does precisely nothing. Also, you create a variable example and assign a dom element to it, then you immediately overwrite it with a (noop) function result. There are additional issues with your code as well.
My advice: slow down, determine what it is you need to accomplish, and take it step by step.
I think this is what you wanted. But not really sure what you are trying to do here.
var begin = 500000;
var stop = 999999;
var arrInt = [];
var CreateArray = function (begin,stop) {
while(begin < stop +1){
arrInt.push(begin++);
}
};
var IntSum = function (ManyInts) {
var sum = 0
var i = ManyInts.length; while (i--) {
sum += ManyInts[i];
}
return sum;
};
var example = document.getElementById("example").innerHTML;
CreateArray(begin, stop);
var saic=IntSum(arrInt);
document.getElementById("example").innerHTML = saic
http://jsfiddle.net/wpnkL6k2/

Categories