I am complete new to open source and would really like to use it more. I installed (x86)node.js from nodejs.org. I launched "Node.js command prompt" from the installed list, and executed node.exe. I am trying to to run some sample javascript. Why is it if I do:
>var life = 11;
undefined
^^^^^^^^^
why am I getting this message?
or
>a = [1,2,3]
[1,2,3]
>a.forEach(function (v) {console.log(v);});
1
2
3
undefined
^^^^^^^^^
//still get this, even though the script executed??
>
The console prints the return value of your script, which is undefined
Write this:
"hello";
And press enter. Now the return value should be "hello" and not undefined.
You could also do;
var life = 11; life;
undefined is just the return value of the statements you executed. Only meaningful/useful if you've executed a function, really.
Related
In the Kotlin REPL, I tried to print an immutable value like this:
val a:Int
a = 5
print("Value of a is: ${a}")
When I execute above code in Android Studio(3.2) with Kotlin(1.2.71) it's thrown an error:
error: captured member values initialization is forbidden due to possible reassignment
a = 5
^
But when I run same part of code at play.kotlinlang.org then it executed successfully and print the output.
<iframe src="https://pl.kotl.in/SJC7APVo7"></iframe>
If I do same thing with mutable valiable in Kotlin REPL then it's works fine:
var a:Int
a = 5
print("Value of a is: ${a}")
Output - Value of a is: 5
So why I am getting above error in Kotlin REPL and also only for immutable variable?
Property of immutable variable is that it can only be assigned once. In your case, compiler does not like the possibility of immutable variables being assigned more than once.
So, this should definitely work:
val a:Int = 5
print("Value of a is: ${a}")
As to why it is behaving differently in 2 places, it depends on the code around it. If a compiler has an easy way to make sure that value can be assigned only once, then it will allow it.
For eg:
val a: Int
if(*condition*) {
a = 5
}
else {
a = 6
}
will be allowed, although "a" is assigned 2 times.
I'm trying to print a gsub string (in Lua) to stdout here how my code look like.
print('string.gsub(\'VEECTORY\',\'EE\',\'I\') =>', string.gsub('VEECTORY','EE','I'))
Everytime I run this, although I get the desired result but I see 1 appearing in the output.
So, the output look like this.
string.gsub('VEECTORY','EE','I') => VICTORY 1
I'm unable to understand what does that 1 stand for but if I used a variable I don't see that 1 anymore.
local replace_string = string.gsub('VEECTORY','EE','I')
print('string.gsub(\'VEECTORY\',\'EE\',\'I\') =>',replace_string)
I get output as
string.gsub('VEECTORY','EE','I') => VICTORY
I also notice that when I run the above code in Lua console
i.e this code
local replace_string = string.gsub('VEECTORY','EE','I')
print('string.gsub(\'VEECTORY\',\'EE\',\'I\') =>',replace_string)
I get the output as nil
What am I missing ?
string.gsub has two return values. The first is result string, while the second is the total number of matches that occurred.
In your example:
string.gsub('VEECTORY','EE','I')
The second return value is 1 because the substitution happened once.
When you assign the result as:
local replace_string = string.gsub('VEECTORY','EE','I')
The first return value is assigned to replace_string, while the second return value is discarded.
You can get the second return value explicitly by:
local replace_string, num = string.gsub('VEECTORY','EE','I')
Finally, in interactive mode, each line is a chunk by itself, so the local variables are out of scope in the next line, therefore you saw replace_string becomes nil. If you use global variables:
replace_string = string.gsub('VEECTORY','EE','I')
print('string.gsub(\'VEECTORY\',\'EE\',\'I\') =>',replace_string)
The output will be as expected in interactive mode as well.
I'm currently learning node.js, and I was just curious what that meant, I am learning and could you tell me why this code does what it does:
var result = 0;
for (var i = 2; i < process.argv.length; i++){
result += Number(process.argv[i]);
}
console.log(result);
I know it adds the numbers that you add to the command line, but why does "i" start with 2? I understand the for loop, so you don't have to go into detail about that.
Thank you so much in advance.
Do a quick console.log(process.argv) and you'll immediately spot the problem.
It starts on 2 because process.argv contains the whole command-line invocation:
process.argv = ['node', 'yourscript.js', ...]
Elements 0 and 1 are not "arguments" from the script's point of view, but they are for the shell that invoked the script.
It starts with 2 because the code will be run with
node myprogram.js firstarg secondarg
So
process.argv[0] == "node"
process.argv[1] == "myprogram.js"
process.argv[2] == "firstarg"
Online docs
Your program prints the sum of the numerical values of the "command line arguments" provided to the node script.
For example:
$ /usr/local/bin/node ./sum-process-argv.js 1 2 3
6
From the Node.js API documentation for process.argv:
An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.
In the above examples those values are:
process.argv[0] == '/usr/local/bin/node'
process.argv[1] == '/Users/maerics/src/js/sum-process-argv.js'
process.argv[2] == '1'
process.argv[3] == '2'
process.argv[4] == '3'
See also the Number(...) function/contructor for JavaScript.
In the node.js, the command line arguments are always stored in an array. In that array, the first element is the node command we refer to because we begin the command line with word “node”. The second element is the javascript (JS) file we refer to that often comes after the node command.
As we know, in the first element in JS array begins from zero, the second element is 1, and it goes 2, 3, 4 and on. Let’s name this array process.argv and add command line arguments x and y. Then, this is how we are going to call these elements:
var process.argv = ['node', 'file.js', ‘x’, ‘y’];
var process.argv [0] = node;
var process.argv [1]= file.js;
var process.argv[2] = x;
var process.argv[3] = y;
In short, element 0 and 1 are native to node.js, and we don't use them when we write any command line argument. That's why we ignore 0 and 1 and always begin from 2.
If we want to loop through the command line arguments, again we have to start from 2. Here is what we use for looping.
for (i = 2; i < process.argv.length; i++){
console.log(process.argv[i]);
}
My answer is not about on how process.argv works -'cause there is a lot of answers here-, instead, it is on how you can get the values using array destructuring syntax.
For example, if you run your script with:
node app.js arthur 35
you can get those values in a more readable way like this:
const [node, script, name, age] = process.argv;
console.log(node); // node
console.log(script); // app.js
console.log(name); // arthur
console.log(age); // 35
You can omit the first and second places of your process.argv, and stay only with name and age:
const [, , name, age] = process.argv;
If you want all the arguments in an array, you can do it using the rest syntax, that collects multiple elements and condenses them into a single element like this:
const [node, script, ...params] = process.argv;
console.log(node); // node
console.log(script); // app.js
console.log(params); // [ 'arthur', '35' ]
and of course, you can omit the first and second places of your process.argv, and stay only with your params:
const [, , ...params] = process.argv;
When you execute it like:
node code.js <argument> <argument>....
It take into account all command line invocation. For process.argv[] array will have ["node","code.js","<argument>",...]
Because of that your arguments that in array start with index 2
process.agrv[i]- basically loops through the command line arguments passed in the terminal while executing the file.
for example- If you run the file as
$ node prog.js 1 2 3 , then process.argv[0]=1 and so on..
I have this error sometimes, though not all of the time, and it's driving me crazy. I don't know if it's a bug or if there is some behaviour or fix that I am not aware of.
I am entering a multiple-line command using :{ and :}, and SOMETIMES when I want to conclude the command, like below, I receive the error as shown below:
*MyModule| :}
unknown command ':}'
use :? for help.
I'd say it works properly 97 percent of the time, but 3 percent of the time I get this situation.
As far as I know, it should always work to type :} to close the multiple line entry, as described here:
http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/interactive-evaluation.html
At the moment, the only way that I know to escape this situation when it happens is ctrl+D, which kills ghci unfortunately.
A. Is this a bug or is there some reason that :} would suddenly become an "unknown command"?
B. If I get to this situation, is there a way to recover without using ctrl+D? It doesn't matter how many times I try :}, it always results in "unknown command" once I have entered this situtation, though what I expect is for this command to close the multiple line entry.
Like it says in the question, this is GHCi, version 7.6.3, on Arch Linux.
As I already noted in the comments, the reason for this behaviour is the GHCi doesn't properly reset the prompt when Ctrl-C is pressed. The source of the problem probably lies in the following code (Taken from ghci-ng):
multiLineCmd q = do
st <- lift getGHCiState
let p = prompt st
lift $ setGHCiState st{ prompt = prompt2 st }
mb_cmd <- collectCommand q ""
lift $ getGHCiState >>= \st' -> setGHCiState st'{ prompt = p }
return mb_cmd
(See InteractiveUI.hs line 712)
If collectCommand throws UserInterrupt, then the line that resets the promt will never be executed. I changed this code to:
multiLineCmd q = do
st <- lift getGHCiState
let p = prompt st
lift $ setGHCiState st{ prompt = prompt2 st }
mb_cmd <- collectCommand q "" `GHC.gfinally` lift (getGHCiState >>= \st' -> setGHCiState st'{ prompt = p })
return mb_cmd
Which fixes the problem.
This is a known bug. Everything is working fine except that ghci sometimes keeps printing the multiline prompt when it should be printing the normal prompt.
I found an strange behavior of coffee compiler on an simple expression, which differs from a interactive compiler reaction at coffeescript.org site.
When I try to compile next string with coffee:
(console.log i; break) for i in [0..10]
I got:
SyntaxError: In repl, cannot use a pure statement in an expression.at SyntaxError (unknown source) ...
But the same expression in interactive compiler at coffescript.org compiled just fine, as expected by me to:
var i, _i;
for (i = _i = 0; _i <= 10; i = ++_i) {
console.log(i);
break;
}
Why coffee don't like () grouping here?
UPD
Another strange thing - it happens not always, sometimes, after a lot of tries and variations, coffee starts to compile absolutely the same expression normally, without errors.
But seems like in interactive mode, coffee fails always.
Another strange thing I found - this error happens only when I use the 'break' keyword. Expression '(console.log i; i+1) for i in [0..5]' works just fine and returns an array.
The problem is that the REPL tries to give you the result of every expression (and save it as _). So internally, it's compiling
_ = ((console.log i; break) for i in [0..10])
which breaks the compiler because you can't use break in a list comprehension.
I would recommend creating myfile.coffee in your favorite editor and running it (coffee myfile.coffee) rather than using the REPL.