0

I have this node.js code:

var timeStamp = function() {
  var d = new Date();
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  var day = d.getDay();
  var hours = d.getHours();
  var minutes = d.getMinutes();
  var seconds = d.getSeconds();
  var milliseconds = d.getMilliseconds();
  var time = year + '.' + month + '.' + day + '. ' + hours + ':' + minutes + ':' + seconds + ':' + milliseconds;
  return time;
};

var tld = function() {
  var domainExtensions = ['.com', '.org'];

  for (var i=0; i<domainExtensions.length; i++) {
  return domainExtensions[i];
  }
};

// print process.argv
process.argv.forEach(function(val, index, array) {
  console.log(index + ': ' + val + '' + tld() + ' █ ' + timeStamp());
});

This outputting:

$ node index.js one two=three four
0: C:\Program Files\nodejs\node.exe.com █ 2015.11.5. 13:45:18:187
1: c:\www\node\command-line-arguments\index.js.com █ 2015.11.5. 13:45:18:191
2: one.com █ 2015.11.5. 13:45:18:192
3: two=three.com █ 2015.11.5. 13:45:18:192
4: four.com █ 2015.11.5. 13:45:18:193

However what I want is to print in the console one console argument with every loop of array:

$ node index.js one two=three four
0: C:\Program Files\nodejs\node.exe.com █ 2015.11.5. 13:45:18:187
1: C:\Program Files\nodejs\node.exe.hu █ 2015.11.5. 13:45:18:187
2: c:\www\node\command-line-arguments\index.js.com █ 2015.11.5. 13:45:18:191
3: c:\www\node\command-line-arguments\index.js.hu █ 2015.11.5. 13:45:18:191
4: one.com █ 2015.11.5. 13:45:18:192
5: one.hu █ 2015.11.5. 13:45:18:192
6: two=three.com █ 2015.11.5. 13:45:18:192
7: two=three.hu █ 2015.11.5. 13:45:18:192
8: four.com █ 2015.11.5. 13:45:18:193
9: four.hu █ 2015.11.5. 13:45:18:193

Is there a way to achieve this?

Thank You for your help!

2 Answers 2

1

try this

var timeStamp = function() {
  var d = new Date();
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  var day = d.getDay();
  var hours = d.getHours();
  var minutes = d.getMinutes();
  var seconds = d.getSeconds();
  var milliseconds = d.getMilliseconds();
  var time = year + '.' + month + '.' + day + '. ' + hours + ':' + minutes + ':' + seconds + ':' + milliseconds;
  return time;
};

//better to be const
var domainExtensions = ['.com', '.org'];

// print process.argv
process.argv.forEach(function(val, index, array) {
  domainExtentions.forEach(function(tld) {
     console.log(index + ': ' + val + '' + tld + ' █ ' + timeStamp());
  });

});
Sign up to request clarification or add additional context in comments.

1 Comment

I've got the following console error: ReferenceError: domainExtentions is not defined. I corrected the mistype. Now I've got another error: ReferenceError: tld is not defined. I corrected it (tdl -> tld) and it works. thanks!
1

You leave the loop when you call return. The tld function always return ".com".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.