DEV Community

Cover image for Chalk Module in JavaScript
ayush65
ayush65

Posted on • Updated on

Chalk Module in JavaScript

Chalk module in Node.js is the third-party module that is used for styling the format of text or create our own theme in the node.js project

For the neog.camp in the mark 1 i learn about it and used in my program .

A small part of program is as follows :-

we can use even const and var as per our choice.

var variable = require('readline-sync');
const chalk = require('chalk');
Enter fullscreen mode Exit fullscreen mode

we have to first write the following code for importing the module
Now ,

var variable = require('readline-sync');
Enter fullscreen mode Exit fullscreen mode

Using readline-sync helps us to get the user input later if we want in our program

# concatenation

It concatenates one string to the end of another string using the ( + ) Sign .
We can use this for welcoming the users.

console.log("wecome " + user )
Enter fullscreen mode Exit fullscreen mode

eg:- In this program we want to check that the user is correct or not about the questions provided .

Now,

const chalk = require('chalk');
Enter fullscreen mode Exit fullscreen mode

chalk is a variable here you can use any name as you want for the variable .

we have to import chalk module is used for providing the color for for the console.log() as we discussed earlier

var username = variable.question('what is your name ? ');

console.log(chalk.blue("welcome",username,"lets play"));
Enter fullscreen mode Exit fullscreen mode

here using the chalk.blue we can get the blue color for the console.log part as :-

output

We can use the chalk module as a variable also :-

const warning=chalk.red;
const welcome=chalk.green;
const endgame=chalk.yellow;
Enter fullscreen mode Exit fullscreen mode

We can use in the console.log() as :-

    console.log(welcome('you are right! '));

    console.log(warning('oops! its wrong'));
Enter fullscreen mode Exit fullscreen mode

We will assign the score for the small CLI app game :-

Functions

           function gamename(playername){
                       console.log(playername)

                     }



            function name(){
                    console.log("Ayush Prakash")
                   }

Enter fullscreen mode Exit fullscreen mode

These are two types of functions one take parameter and second one doesn't take any parameter, what parameter means is to pass a value to that piece of code which that code can implement function includes concepts of global and local variable, “ A variable inside a function stays inside a function unless its global ” that means a variable declared inside a function cannot be used by other functions unless its global or its passed as parameter

Initially the score will be zero .

score = 0;


function playGAme(question,answer){
  var Userans = variable.question(question);

  if (Userans === answer){
    console.log(welcome('you are right! '));
    score = score+ 1;
  }else{
    console.log(warning('oops! its wrong'));

  }

}

Enter fullscreen mode Exit fullscreen mode

for last defining part we can use it with console.log as shown below :-

output

Lists

As we have seen array stores a single value without ref to store value with ref we use dictionary wich consists of key & value pair we can call a value by its key name. it uses 'CURLY BRACKETS {..}'

var obj1 ={
  name:"Ayush",
  age:"20",

}
var obj2 ={
  name:"Prakash",
  age:"15"
}
Enter fullscreen mode Exit fullscreen mode

We can use the user provided data in the list of object and if we have more than one time to do we will use loop Both can be done as :-


var listofstudent = [];

for (let i=0 ; i<5 ; i++) {

var name = variable.question('what is your name ? ');
var unitTest = variable.question('what is your unit test marks ? ');
var preFinal = variable.question('what is your Pre final ? ');
var final = variable.question('what is your Final marks ? ');

console.log('------------------------------------------')
console.log('------------------------------------------')

var tempobj ={
  name: name,
  unitMarks: unitTest,
  preMarks: preFinal,
  finalMarks : final
}
listofstudent.push(tempobj);
}
Enter fullscreen mode Exit fullscreen mode

We are storing the values here unitTest marks and all in the listofstudents list . Later we made temp object and later we push the values in the listofstudents list

We can use that in a function as :-


var obj1 ={
  name:"ram",
  power:"2500",
  yuga:"treta"
}
var obj2 ={
  name:"krishna",
  power:"2325",
  yuga:"dwapar"
}

function playGAme(obj1,obj2){


  if (Number(obj1.power) > Number(obj2.power)){
    console.log(chalk.yellow(obj1.name + ' has more power than '+obj2.name));

  }  
  else if (Number(obj1.power) < Number(obj2.power)){
    console.log(chalk.yellow(obj2.name + ' has more power than '+obj1.name));

  }  

  else{
    console.log('they both have same power')
  }

}

playGAme(obj1,obj2);


Enter fullscreen mode Exit fullscreen mode

thanks for giving your valuable time .

Top comments (0)