Posts

Usestate React

  import   React , {  useState  }  from   'react' ; export   default   function QuizNavBar ({  questions  }) {    const  [ questionIndex ,  setQuestionIndex ] =  useState ( 0 );    const goBack  = () =>      setQuestionIndex (( prevQuestionIndex ) =>  prevQuestionIndex  -  1 );    const goToNext  = () =>      setQuestionIndex (( prevQuestionIndex ) =>  prevQuestionIndex  +  1 );    const onFirstQuestion  =  questionIndex  ===  0 ;    const onLastQuestion  =  questionIndex  ===  questions . length  -  1 ;    return  (      <nav>       <span> Question  # { questionIndex  +  1 }...

Mongoose

  const mongoose = require ( "mongoose" ); mongoose . connect ( "mongodb://localhost:27017/fruitsDB" ); const fruitSchema = new mongoose . Schema ({     name : {         type : String ,         required : true ,     },     rating : {         type : Number ,         min : 1 ,         max : 10     },     sweet : Number }); const Fruit = mongoose . model ( "Fruit" , fruitSchema ); // CREATE const peach = new Fruit ({                             name : "Peaches" ,                             rating : 7 ,                             sweet : 4                         }...

writing data to a database using mongo driver

const MongoClient = require ( "mongodb" ). MongoClient ; // Replace the uri string with your MongoDB deployment's connection string. const url = "mongodb://localhost:27017" ; const client = new MongoClient ( url ); async function run () {   try {     await client . connect ();     const dbName = "newFruits"     const database = client . db ( dbName );     const foods = database . collection ( "fruits" );     // create an array of documents to insert     const docs = [       { name : "watermelon" , healthy : true , rating : 5 },       { name : "mushroom " , healthy : true , rating : 2 },       { name : "mango" , healthy : true , rating : 4 }     ];     // this option prevents additional documents from being inserted if one fails     const options = { ordered : true };         const result = await f...

To read data from database using mongo driver

  const MongoClient = require ( "mongodb" ). MongoClient ; // Replace the uri string with your MongoDB deployment's connection string. const url = "mongodb://localhost:27017" ; const client = new MongoClient ( url ); async function run () {   try {     await client . connect ();         const cursor = client . db ( "newFruits" ). collection ( "fruits" ). find ({});     const results = await cursor . toArray ();     if ( results . length > 0 ) {         console . log ( `Found listing(s)` );         results . forEach (( result , i ) => {                         console . log ( `$_id: ${ result . _id } name: ${ result . name } review: ${ result . rating } ` );                            });     } else {       ...

Template, layout and scope

 Templates:- Prewritten line of codes that a developer or programmer can modify according to his/her needs. The use of templates removes repetition saving a lot of time and making it easier for a developer to create a project. Also, templates are blocks of codes which add feature and content to be render on the page. Layout:- Layouts are files that define the structure of the page such as header, footer, navbar, sidebar, etc Concept of scope:- generally there are two types of scope: 1.) Local: which can only be accessed inside the function they are declared in. 2.) Global: This can be accessed throughout the program. Generally, all data types can be local as well as variable: eg: if var, let, const, are declared inside a function they will be local, and if created outside the function they will be global, but if these three are declared in conditional or loop statements the let will behave differently than var, and const. Meaning in conditional and loop statements the let statement...

To do list (Algo)

 Algorithm for to do list: Step: 1: In terminal initialize npm and install express,body-parse, and ejs. 2: in editor incorporate body-parser and express. 3: Bind express to a constant app 4: Using app.set incorporate ejs with express.                           app.set("view engine", "ejs); 5:using app .use incorporate body-parser(now depreceated), so we can use express instead. 6:using app.get define route and has a call back which will hold the data of get.     6a:  inside call back:-             6a1: create a variable for date                6a2: create an object which will contain the date day month and year to be displayed.               6a3:create a variable which will convert the date toLocaleDateString (Date string) 7: using res.render, render the template (list) and i...

To do list (without styling and css)

the html file is written in the file having extension as ejs rather than HTML because it can include embedded javascript logic inside the file itself. html of ejs todo list:- <!DOCTYPE html> <html lang="en" dir="ltr">   <head>     <meta charset="utf-8">     <title>To do List</title>   </head>   <body>     <% if(kindOfDay==="Saturday"||kindOfDay==="Sunday"){ %>     <h1 style="color:Purple"> <%= kindOfDay %> </h1>     <% } else { %>       <h1 style="color:Blue"> <%= kindOfDay %> </h1>     <%  } %>     <ul>     <% for (var i = 0; i<newListItems.length; i++){ %>       <li> <%= newListItems[i] %> </li>     <% } %>       </ul>     <form class="" action="/" method="post"> ...