I am new to node.js so please be gentle. I am trying to run a simple piece of code that converts rgb values to hex values. However, I can't seem to execute the program in the terminal without appending single quotation marks to my arguments.
When I type in:
node index.js rgbhex rgb(255,255,255)
in my bash- terminal I am getting an error of:
syntax error near unexpected token `(`
However, when I do the following with single quotation marks around my arguments:
node index.js 'rgbhex' 'rgb(255,255,255)'
The code runs normally (i.e output is #ffffff). My question is why do I need to include the single quotation marks in my arguments? Any guidance is appreciated.
my index.js for reference :
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
//More comfortable solution
let red;
let green;
let blue;
if(process.argv[2]!=='rgbhex'){
red = Number(process.argv[2]);
green = Number(process.argv[3]);
blue = Number(process.argv[4]);
} else {
let rgbInput = process.argv[3];
let regexTest = /[0-9]+/g;
let arrayOfRgbValues = rgbInput.match(regexTest);
red = Number(arrayOfRgbValues[0]);
green = Number(arrayOfRgbValues[1]);
blue = Number(arrayOfRgbValues[2]);
}
console.log(rgbToHex(red,green,blue));