When I run the following code in node.js I get the following error message:
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
//Running this code causes program to run out of memory.
for(var i = 0; i < files.length; i++){
readFile(files[i]);
}
//log each line of file to console
function readFile(data){
var lines = fs.readFileSync(data).toString().split('\n');
for(var k = 0; k < lines.length; k++){
console.log(lines[k]);
}
}
But, if I change the readFile function and take the console.log(lines[k]) out of the for loop, the fatal error goes away. Why is that? And how can I fix the original code so it stops running out of memory?
function readFile(data){
var lines = fs.readFileSync(data).toString().split('\n');
var string = ''
for(var k = 0; k < lines.length; k++){
//console.log(lines[k]);
string += lines[k];
}
console.log(string); //log the same string outside the loop and the fatal error goes away
}
do npm install async --save
var async = require('async');
async.eachSeries(files , filesIteration , finishIteration);
function filesIteration(file , callBack){
readFile(file , callBack);
}
function readFile(data , cb){
var lines = fs.readFileSync(data).toString().split('\n');
for(var k = 0; k < lines.length; k++){
console.log(lines[k]);
if(k === lines.length - 1){
cb();
}
}
}
function finishIteration(){
console.log('all files processed');
}
Related
I keep getting an error message in my web page console when testing out this loop; "ReferenceError: Can't find variable: sumArray".
My first loop worked just fine and resulted in a "true" message.
//*** isUniform() ***
function isUniform(arr){
var first = arr[0];
for(var i = 1; i < arr.length; i++){
if(arr[i] !== first){
return false;
}
}
return true;
}
But my second loop worked just fine and resulted in a "ReferenceError: Can't find variable: sumArray" message.
// *** sumArray() ***
function sumArray(arr){
var total = 0;
arr.forEach(function(element){
total += element;
});
return total;
}
Im using node.js to list out items from a txt file. I have it to print them out from terminal, however i want it to accept more parameters, as of now it accepts them but only prints one out(the last one) i want it to print each item in the parameter and list them out.
// Make sure we got a filename on the command line.
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME');
process.exit(1);
}
// Read the file and print its contents. And split into an array after each space
var fs = require('fs') , filename = process.argv[2];
var array = fs.readFileSync('dependencies.txt').toString().split('\n');
//console.log(array[0]);
if(process.argv.length >= 3){
for(var j = 3; j < process.argv.length; j++)
var test = process.argv[j];
functionName(test);
}
function functionName(test){
for(var i = 0; i < array.length; i++ ){
var pattern = /([^\s]+)/g;
var line = pattern.exec(array[i]);
if(line && test == line[0]){
console.log(array[i]);
}
}
}
//fs.readFile(filename, 'utf8', function(err, data) {
// if (err) throw err;
// console.log('OK: ' + filename);
// console.log(data)
//});
how it prints out
A for loop can only contain one statement, so if you need more than one, you'll have to wrap it in a block statement.
(Thanks #paulpro)
I would just add a block statement for all for loops, it's more readable that way unless it was short enough to be readable on one line.
for(var j = 3; j < process.argv.length; j++) {
//var test = process.argv[j];
functionName(process.argv[j]);
}
let len = process.argv.length, j = 3;
for (;j<len;j++) functionName(process.argv[j]);
I am using UglifyJS to minify js. I've noticed that:
var start = 0;
for( var t = start; t<k.length; t++)
The t is replaced with
var t=!0
What is going on here? I know !0 === true, but this is throwing errors for IE8. It doesn't throw any error for other modern browsers but I am not sure they are actually working as expected.
Edit: example here
var start = 0;
function scan() {
var hello = true;
for (var j = start; j < 3; j++) {
}
return hello;
}
After minify using Uglify
function scan(){for(var a=!0,b=start;3>b;b++);return a}var start=0;
So, i'm working on a 'for' loop that will identify my name, Andrew, and push it into an array, but there's something wrong with it
/*jshint multistr:true */
var text = ("Andrew is really awesome and Andrew should be working on the project, but there is honestly nothing for Andrew to do.");
var myName = ("Andrew");
var hits = [];
for (var i = 0; i < text.length; i ++) {
if (text[i] === "A") {
for (var j = i; i + nyName.length; i ++) {
hits.push(text[j]);
}
}
}
Also, the second loop is supposed to stop when it reaches the end of myName.
You're using JSHINT, so just read the error messages and it'll tell you exactly what's wrong.
Errors:
Line 7: for (var j = i; i + nyName.length; i ++) {
'nyName' is not defined.
Line 3: var myName = ("Andrew");
'myName' is defined but never used.
JSHINT isn't much good if you don't pay attention to what it's telling you.
Also, your inner loop looks odd.
for (var j = i; i + nyName.length; i ++) {
Seems like it'll cause an infinite loop. You're perhaps wanting j with a different condition.
You misspelled myName in your for loop syntax and typed nyName instead, so chances are the script dies as soon as it hits that line.
A typo in the for loop that wants to refer to myName would appear to be a big problem:
for (var j = i; i + nyName.length; i ++)
^
The misspelled myName isn't the only part that fails. You for loop will never end the loop because i + myName.length will always evaluate to true. You also need to increase the value of j or it will always get the character at index i.
Here's the corrected loop.
for (var i = 0; i < text.length; i ++) {
if (text[i] === "A") {
for (var j = 0; j < myName.length; i++, j++) {
hits.push(text[i]);
}
}
}
var markers3 = [];
for (var i = 0; i < 1964; i++) {
var dataPhoto = data8.weblandmarks8[i];
if(data8.weblandmarks8[i].ip2==data8.weblandmarks8[i+1].ip2 )
{
var k=i+2;
while(1)
{
if((data8.weblandmarks8[k].ip2) == (data8.weblandmarks8[i].ip2))
k++;
else break;
}
for (var j = i; j < k; j++)
{
var latLng = new google.maps.LatLng(data8.weblandmarks8[j].latitude,data8.weblandmarks8[j].longitude);
var marker = new google.maps.Marker({position: latLng,icon: webicon,title:data8.weblandmarks8[j].webaddress});
markers3.push(marker);
}
i=k-1;
}
}
google maps application
I get the following error in firefox
Error: data8.weblandmarks8[k] is undefined
Source File: file:///Applications/MAMP/htdocs/geo/js/myscript.js
Line: 140
I have defined K but why does firefox say that the json data accessed with it is undefined?
the same data can be accessed with i variable.
edit:
I have a json data file which I am accessing by
its working
but the error is with that specific line only
This calls for some basic debugging first: For what value of k does it fail? What happens if you try to address that value directly?
I'm fairly sure k (being i+2) simply reaches the end of the list at some point - as it stands, the code needs 1966 elements to be present in order to work.