- Go to repl.it and sign up on it
- Create a repl for node js
- Write console.log("Your name here")
- Take input from user:
- prompt("Give me your name")
- Comment using //, or you can use "ctrl + /" to comment whenever you want
- Search for "npm readline-sync" in search bar (also read about it on documentation (documentation is a book about any tool, written by people who created that tool))
- You can take input using readlineSync
- var readLineSync = require('readline-sync');
- var userName = readLineSync.question("May I have your userName");
- Now, output that userName that you took using console.log(userName);
- Welcome your user
- var welcomeMessage = "Welcome! Simran";
- console.log(welcomeMessage)
- Putting placeholder (Placeholder means anything that will act as landmark for some particular information you're looking for.)
- var welcomeMessage = "Welcome " + userName; //string concatenation
- console.log(welcomeMessage);
- Use shortcuts ctrl + / (to comment)(as mentioned earlier)
- Play with programs
- Programming is input + process + output (Used classic potato chips example)
- Quiz part
- Conditional programming (if - else)
- Save previous 5 programs with name "upto ex 5"
- fork and name "ex 6 onwards" (fork means making a copy of current program and working on it also, making sure, previous versions are not affected )
- Program's demand : right / wrong using age input (So, are you able to guess my age correctly? Let's see that in upcoming program.😏)
26. var readlineSync = require("readline-sync");
var score = 0;
27. var userAnswerAge = readlineSync.question("Am i old than 25?"); //input
28. //process
29. if(userAnswerAge === "yes")
30. {
console.log("You are right") //output
score = score + 1;
console.log("Score is "+ score);//output
}
31. else
{
console.log("You are wrong") //output
score = score - 1;
console.log("Score is "+ score); //output
}
-
He assigned us the link. And asked to play with it. (Repl link in description of video of Tanay Pratap's channel under video named "First CLI app: Intro to programming".)
-
We solved the task assigned to us by Tanay. We added on more question and changed score.
-
We start with function
-
Functions
-
syntax on neogcamp site
39. function add (numberOne, numberTwo)
{
var sum = numberOne + numberTwo;
return sum;
}
//use the function
var result = add (2,5);<br/>
console.log("The sum of 2 and 5 is: "+ result);
40.Operators
41. /* + - / * */
- livework: function to takes 2 numbers, multiplies them and gives answer;
function mul(number1, number2):
{
var product = number1*number2;
return product;
}
var result = mul(45,23);
console.log("The product of two numbers is: "+result);
43.// Function q n a
var readlineSync = require("readline-sync");
var Score = 0;
function play(question,answer)
{
var userAnswer = readlineSync.question(question);
if(userAnswer === answer)
{
console.log("You were right");
score = score + 1;
}
else
{
console.log("You were wrong");
score = score - 1;
}
}
// Calling the function
play("National bird of India? ", "Peacock");
play("National Anthem of India", "Jan gan man");
console.log("Your score is: ", score);
- for loop basics
45. for(initialCondition, exitCondition, numberOfTimes)
{
/*Processing*/
/* output/return */
}
46. // print your name 5 times
for(var i=0; i<5; i=i+1)
{
console.log("Simran");
}
- // using for loop bring different questions every time new question should come. This one is DIY
48.This one is another question as part of livework. Break question:
- Run a loop and print the sum of two numbers.
- First number being 22 always, second number being the current value of loop.
- Run 10 times.
- Print should be: 22,23..
49. for(int iteration = 0; iteration < 10; iteration = iteration + 1)
{
var result = 22 + iteration;
console.log("Result after iteration "+ (iteration + 1) + " is "+result);
}
50. /*
Three kinds of brackets:
() => Call a function
{} => When you have to write a block of code.
[] => Used to access array elements.
*/
- Array basics
52. Steps to follow:
i. Prepare a list of grocery items to buy.
ii. Add 5 items. Print the first item on the list.
iii. Print the 3rd item on the list.
iv. Print the last item on list.
53. var groceryList = ["we","MohanThal","soap",300,"eaw"];
54. console.log(groceryList[0]);
55. console.log(groceryList[2]);
56. var howLongIsThisArray = groceryList.length
console.log(howLongIsThisArray)
console.log(groceryList[groceryList.lenth-1])
57. for(var i=0; i<groceryList.length; i++)
{
console.log(graoceryList[i]);
}
- Objects
59. /*Think about Superman
name: "superman"
powers: "flight"
costumeColor: "blue"
*/
60.var superman =
{
name: "superman",
power: "flight",
costumeColor: "blue",
strength: 10000,
stealth: 0
}
var batman =
{
name: "batman",
power: "martialArts",
costumeColor: "black",
strength: 100,
stealth: 100;
}
console.log(superman.strength)
console.log(batman.strength)
61. Objects in JS:
{
key : value
}
- Try to print superman's strength, batman's strength, compare both strength
- Steps to follow:
- Create an array of super hero objects.
- Then print name and costume color of each super hero. Topics covered here: array, for loop, objects, function call. This one is DIY
64.Final game
65. var readlineSync = require("readLine-sync");
var score = 0;
var userName = readlineSync.question("What is your name? ");
console.log("Welcome "+userName+" to DO YOU KNOW <insert-anything-here>?");
//play function
function play(question,answer)
{
var userAnswer = readlineSync.question(question);
if(userAnswer === answer)
{
console.log("right!");
score = score + 1;
}
else
{
console.log("wrong!");
score = score -1;
}
console.log("Current score: "+ score)
console.log("--------------------------------")
}
var questionOne =
{
question: "where do I live? ",
answer : "Silvassa"
}
var questionTwo =
{
question: "My favorite food? "
answer: "Home cooked food"
}
var questions = [questionOne, questionTwo];
_//loop_
for(var i=0; i<question.length; i++)
{
var currentQuestion = questions[i];
play(currentQuestion.question, currentQuestion.answer)
}
- console.log("You scored: ",score)
- Do the homeworks assigned. Check neogcamp site. Do homeworks sincerely.
This one is till first break.