Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8884

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T05:16:07+00:00 2024-11-28T05:16:07+00:00

My tidbits of learning JavaScript : CHALK & readlineSync

  • 60k

So, you finally landed on my first blog post. Welcome!πŸŽ‰

This tutorial is intended for beginners.
If you've got some experience, read this like you're doing code reviews and put your thoughts in the comments below.
Lets start, beast mode.

JavaScript

JS is amazing, it's syntax is like C++ or Java. It can be used to write apps for web, desktop and android as well. Did I mention it's amazing?

I will be using repl.it with node.js runtime.
Tip: Use Ctrl + Enter to run the program in repl.it.

Printing to the console

To print to the console:

console.log("Hello world!"); 
Enter fullscreen mode Exit fullscreen mode

Output:
screenshot

Print a variable:

var x = 2020; console.log("The year is: " + x); 
Enter fullscreen mode Exit fullscreen mode

Output:
Screenshot

But it's very black and white. Lets style it with colors using a package called chalk.

Whats CHALK?

It is:

Terminal string styling done right

We can use it to color console outputs. But first we need to install it. Here, repl.it does a great job. It installs the required packages automatically for you.

Start with including the chalk package:

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

Now, to print in green:

console.log(chalk.green("Its green!")); 
Enter fullscreen mode Exit fullscreen mode

Output:
Screenshot

To underline text:

console.log(chalk.underline.blue("Its underlined and its blue!")); 
Enter fullscreen mode Exit fullscreen mode

Output:
Screenshot

And much more. Take a look at chalk's npm docs.

Accepting user input

We know how to print to the console. What if we need some user input? readlineSync is a library made just for that.

Lets include the package:

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

Now we can take user inputs like:

var userName = readlineSync.question("Enter your name: "); 
Enter fullscreen mode Exit fullscreen mode

Output:
Screenshot

We got the input in the variable userName. Lets print it in color!

console.log("Welcome " + chalk.yellowBright(userName) + "!"); 
Enter fullscreen mode Exit fullscreen mode

Output:
Screenshot

Arrays

An array is a collection of variables which can be accessed by using the index.
Simple list of song genres:

var songGenres = ["Blues", "RnB", "Pop", "Rock"]; 
Enter fullscreen mode Exit fullscreen mode

Lets print the first and third genres:

//first genre console.log(songGenres[0]);  //third genre console.log(songGenres[2]); 
Enter fullscreen mode Exit fullscreen mode

Output:
Screenshot

The index numbering starts with 0. So, here we have 0, 1, 2, 3 as index numbers.


Now, lets put everything together and build a program to choose a song.

Include required packages:

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

Define an array to store songs as a list:

var songs = ["Skyfall", "Beautiful times", "Bailando", "Grenade"]; 
Enter fullscreen mode Exit fullscreen mode

These are my favorites by the way.

Get the user's name as input:

var userName = readlineSync.question("Whats your name? "); 
Enter fullscreen mode Exit fullscreen mode

Now, we will be using a readlineSync's method called keyInSelect to select a song from the list.

var favSong = readlineSync.keyInSelect(songs, "Choose a song:");  console.log(chalk.red("You chose " + songs[favSong] + "!")); console.log(chalk.green("The tutorial is over. Enjoy the song!")); 
Enter fullscreen mode Exit fullscreen mode

Output:
Screenshot

Thats all for now

The best way to learn and retain information is to teach it to someone or document it. I have tried to keep this post just like that: simple, fun and exciting.

So, get on your keys…get set…and code!

I will be posting more about JavaScript. We haven't even scratched the surface yet. There's a lot to learn!

Please comment below your suggestions or doubts. I will try my best to answer the queries.

P.S.:I can make emojis italic.πŸ‘πŸ€˜

javascriptnpmreplitwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.