I want to post a few personal how 2's
that belong to the webkit way of doing things
Run JavaScript jsc from the command line in Linux
tested on the raspberry pi3
Dependency
1.) Get the javascript engine for gtk3 and webkit
that is if you are using a debian based system
sudo apt-get install libjavascriptcoregtk-3.0-bin
man jsc
and https://trac.webkit.org/wiki/JSC
I ported an example from node syntax to jsc syntax
https://www.pcsuggest.com/run-javascript-from-command-line-linux/
jsc is a wild non conformant creature
notice
The way you pass arguments prefixed with -- then the args
usually argv 0 is the command
then argv 1 is the name of the script
then argv 2 and more passed to the functions
that is a set standard
notice
JRC breaks all those rules !
jrc arg 0 = argv[2] standard array syntax
jrc arg 1 = argv[3] standard array syntax
# NODE:
A very simple BMI calculator in javascript, not fault tolerant
var mass = +process.argv[2]; var height = +process.argv[3]; BMI = mass / Math.pow(height, 2); console.log(BMI);
it expects you pass 2 arguments
weight in kilos and height in meters
This is my port of the node example using jsc
var mass = arguments[0]; var height = arguments[1]; BMI = mass / Math.pow(height, 2); //debug alias PRINT to console debug(BMI);
I added some comments and a test to
check if you passed arguments
now in the linux terminal run
jsc bmi_calc.js -- 95 1.92
name this code below bmi_calc.js
bmi_calc download it
//-------------------------------------------- // bmi_calc.js -- 95 1.92 //-------------------------------------------- // argument 0 is 95 // argument 1 is 1.92 // added a test if arguments were passed if (!arguments[0]) { print('usage:\n # jsc bmi_calc.js -- 1 2'); quit(); } var mass = arguments[0]; var height = arguments[1]; BMI = mass / Math.pow(height, 2); //debug alias PRINT to console debug(BMI); debug(arguments[0]); debug(arguments[1]);
NODE | JSC |
---|---|
var mass = +process.argv[2]; | var mass = arguments[0]; |
var height = +process.argv[3]; | var height = arguments[1]; |
BMI = mass / Math.pow(height, 2); | BMI = mass / Math.pow(height, 2); |
console.log(BMI); | debug(BMI); |