Web WB
Web WB
Web WB
useEffect(() => {
fetchData(setData);
}, []);
❓ 2. What is ECMA script ? What is the difference between Javascript & ECMA
script ?
The let variables only work inside the block where they are defined (they are
blocked-scoped variables). On the other hand, the var variables are function-scoped
variables that, if defined inside a function, are accesible anywhere in that
function (including nested blocks or loops), and if they are defined globally, they
are accesible globally (in any function, any loop, any block of code).
Also, let variables are a EcmaScript6 features, and var variables are a ECMAScript1
feature (the let and const are modern alternatives included in more recent
standards, in order to have better control over variable scoping and to prevent
common programming errors).
❓ 4. Write an example where using the var declaration instead of the let could
create a hard to debug code.
var let
function printNumbers(){
var num = 10;
if (true){
var num = 20;
}
console.log(num)
}
if (true){
let num = 20
}
console.log(num);
}
printNumbers() // Output: 10
❓ 5. Give a practical example where you would use the reduce function in
javascript.
console.log(sum); // Output: 15
❓ 6. Give a practical example where you would use the map function in javascript.
We can use map to transform each element of an array into another (Celsius degrees
into Fahrenheit degrees).
const people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
{ name: 'Dave', age: 40 },
{ name: 'Eve', age: 45 },
];
*A web server can refer to hardware or software or both. Commonly, a web server is
a software application that receives requests from the clients, processes the data
and sends back responses for requested content.
npm is a package manager for Node.js that allows developers to install and manage
third-party packages and dependencies for their Node.js projects. Developers can
specify the dependencies required for their project in the package.json file, and
then use the npm install command to automatically download and install all the
required packages and their dependencies.
❓ 12 What is the difference between the depdendencies & devDependencies in a
package.json file?
If fetch() was not asynchronous, the program would have to wait for the fetch
response and the code would not be executed until getting that response, which
would make the page slow and unresponsive.
❓ 14 What benefits would bring to a developer to use the Postman application?
Postman is helping developers to save time and effort when building and testing
API. Using Postman, you can test, you can collaborate, you can debug, you can
understand easier the API documentation.
❓ 15 List the parts of the URL.
Here are the most relevant HTTP request components (including headers):
request URL
request body
❓ 19 How does an HTTP Response look like? What are the most relevant HTTP header
fields?
response body
It is better to put the node_modules folder in the .gitignore file because it gets
too much space from the Git repository (the folder has a large size). It is enough
to have them in the "package.json" so that you can use npm install and get them
again when you need them. Also, it is better for the security of the project to
include the "node_modules" in the ".gitignore" file.
Rest API: Serve and Fetch
❓ 21 Why is it recommend for a developer to use the http methods get, put, delete?
HTTP methods are secure and efficient ways to work with data on a server.
We use the GET method to retrieve data.
We use the PUT (or PATCH) method to update data.
We use DELETE method to delete data.
❓ 22 How does a POST request look like when it is made from a web browser (on the
frontend written)?
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br>
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea><br>
<button type="submit">Submit</button>
</form>
❓ 23 What is an API?
REST APIs or RESTful APIs follow the principles and procedures of the
Representational State Transfer architectural style for web services.
The client (browser/ device) communicates with the server (application/ service) in
a flexible and standardized way, using almost any programming language and data
formats.
❓ 25 What is JSON and how do we use it?
API versioning allow developers to make changes to an API without breaking the
clients' apllications that rely on that API.
The changes can be integrated in URL versioning (include change in API Url), query
versioning (include change in API parameter), header versioning (include change in
header) and media type versioning (include change in media type of response).
❓ 27 Give 3 examples of HTTP response status codes. Explain what each number means.
From 201 to 299: message indicates succes in retrieving the data. Example: 200 OK.
From 401 to 499: message indicates error on client side and that the server is
unable to find the requested data. Example: 404 not find.
From 501 to 599: message indicates error on server side. Example: 500 Internal
Server Error.
Advanced JavaScript
❓ 28 How does the ternary operator looks like in javascript?
We can use import statement to import a function from another module (from which we
export the function using the export statement).
Example from Stackoverflow.com:
models/course.js
export function Course() {
this.id = '';
this.name = '';
};
models/student.js
import { Course } from './course.js';
window.onload = function () {
var x = new Student();
x.course.id = 1;
document.getElementById('myDiv').innerHTML = x.course.id;
}
</script>
❓ 30 What is a shallow copy on an object?
A shallow copy (or shallow clone) of an object can be created with the spread
operator (...) and it will generate a new object which properties will use the same
memory space as the ones of the original object. This way, if we change a property
value of the copy we also change the property value of the original.
Another way to create a shallow clone is with Object.assign() or slice().
In order to create a copy that doesn't use the same memory space, we need to create
a deep copy (deep clone), using the JSON.parse() and JSON.stringify methods.
❓ 31 What is a callback function? Tell some examples of its usage.
Example:
document.querySelector('button').addEventListener('click', function() {
// Do something when the button is clicked
});
const numbers = [1, 2, 3, 4, 5];
console.log(doubledNumbers);
❓ 32 What is object destructuring in JavaScript?
According to Tutorialspoint.com, JavaScript Object Destructuring is an expression
which allows us to access the data from objects and assign it to new variables.
Through this object destructuring, we can create variables easily from the object’s
properties.
Syntax:
const person = {
name: 'John Doe',
age: 30,
address: {
street: '123 Main St',
city: 'Anytown',
state: 'CA',
zip: '12345'
}
};
With the help of array destructuring we can extract element from an array and
assign them to new variables at the same time.
Example:
The spread operator (...) allows us to create a shallow copy of an array or object
or iterable at at the same time to expand it in individual elements.
Example:
function sum(a, b, c, d, e) {
return a + b + c + d + e;
}
console.log(result);
❓ 35 What are the differences between the arrow function and the regular function?
Code coverage indicates the proportion of lines, functions and branches of the code
that were covered by a test. It helps to measure how much of the code has been
tested.
❓ 40 What is a test case? What is an assertion? Give examples!
A test case is a scenario that defines the input values, expected output, and any
other relevant conditions or requirements for a test.
An assertion is a statement that checks whether an expected condition is true or
false.
Example:
Test Case:
A login form that accepts a username and password.
Assertions:
When the correct username and password are entered, the user should be logged in
successfully.
When an incorrect username or password is entered, the user should not be logged
in.
When the login form is submitted without any input, an error message should be
displayed.
❓ 41 What are the unit testing best practices? (Eg. how many assertion should a
test case contain?)
include a single assertion in one test method (test one thing at a time; one
assertion per test case)
use clear test cases
run tests frequently
write tests during development
❓ 42 What is arrange/act/assert pattern?
In jest, setup and teardown are functions that we can use to define actions to be
performed before and after running every test.
❓ 45 Give an example when you would use in jest the toBe & toEqual assertions.
The toBe assertion tests for strict equality between two values.
Example:
Some benefits of using components instead of writing all code in one place:
code is more suple and easy to follow
it is easier to test/ debug the code
it is easier to re-use the code
it is easier to collaborate
makes things more clear and helps the logic
it is easier to make changes in the code
❓ 47 What is the difference between Element and Component?
Usually, an element refers to a HTML tag, whistl a component is more complex and
dynamic and encapsulates more elements.
❓ 48 How do you pass values between components in react?
When working with a list and using .map() we need to include a key so that React
keeps track more efficiently of what we changed, added or deleted from the list.
Example:
function MyList(props) {
const items = props.data.map((item) => (
<li key={item.id}>
{item.name}
<ul>
{item.children.map((child) => (
<li key={child.id}>{child.name}</li>
))}
</ul>
</li>
));
return <ul>{items}</ul>;
}
❓ 50 How does a child component pass data to it's parent component?
To pass data from a child component to its parent component, we can use props or
useContext.
❓ 51 Write the code to create in JSX an HTML DIV element that has the innerText the
contents of the variable let name = 'Andrew'
Example:
function MyComponent() {
let name = 'Andrew';
return (
<div>{name}</div>
);
}
Example:
function MyComponent() {
let names = ["Mathew", "John", "Maverik"];
return (
<ul>
{names.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
);
}
Example:
import React from 'react';
function MyComponent() {
return (
<div style={{ backgroundColor: 'red' }}>Hello, world!</div>
);
}
Unit testing is designed to test individual units or components, isolated from the
rest of the application.
❓ 56 What does mocking mean from a testing perspective? Give an example when you
would use it.
When testing, mocking means to create a simulated version for the data that the
code we want to test relies on.
A frequent example of recently used mocking was to add data to see it create-read-
update-delete operations work as expected.
❓ 57 How do you test that function was called at least 2 times using jest?
Example:
function myFunction() {
// ...
}
// Test case
test('myFunction is called at least 2 times', () => {
// Call the function twice
myFunction();
myFunction();
When we update an array, the state variable is updated too and a new copy of the
variable is created instead of changing the original, in order to be able to re-
render the component.
❓ 61 Describe what techniques or tools you use to debug a react app.
A functional component is an easy to read, write and test component that consists
of a function where we can use React hooks (useState, useEffect).
A class component usses class keyword and extends React.Component and uses render()
method.
❓ 63 Name 3 lifecycle states in a react functional component.
useState
useEffect
useContext
❓ 64 What is conditional rendering in react ? Give an example.
Conditional rendering means to render different parts of the code based on certain
conditions.
Example from legacy.reacjs.org:
render() {
const count = 0;
return (
<div>
{count && <h1>Messages: {count}</h1>}
</div>
);
}
❓ 65 Write the code that prints to the console component destroyed when the
component it is part of is removed from the DOM tree.
function DestroyedComponent() {
useEffect(() => {
return () => {
console.log('component destroyed');
};
}, []);
return (
<div>
<h1>Destroyed component</h1>
<p>This is my component</p>
</div>
);
}
return (
<div className="App">
<p> value of count: {count} </p>
</div>
);
}
The useEffect hook should have a dependency array, in this case [count], in order
to only render when the count is changed.
❓ 67 Why is there an infinite loop in this code?
function App() {
const [count, setCount] = useState(0);
return (
<div className="App">
<p> value of count: {count} </p>
<button onClick={incrementCount}>Increment</button>
</div>
);
}
Mongo & mongoose
❓ 68 What is a database schema?
A database schema in MongoDB describes the structure of data and specifies hot to
store the data, what type is the data and so on.
❓ 69 Why is the id unique in a database?
Every ObjectId in MongoDB is generated automatically and is unique, because the _id
field is the primary key for each document, in order to efficiently retrieve the
document from a collection.
❓ 70 What are the advatanges & disadvatages of using lean() function in a mongo
query?
Passwords should not be stored in plain text, but should be stored in a database
after being encrypted by a reversible algorithm (source: Vaadata.com).
❓ 76 What are encryption and decryption?
Decryption transforms the encrypted text back into readable form. (encrypted text
=> plain text)
❓ 77 What is hashing?
in path for environmental variables that are set outside the app code
encrypted - stored in a database and store the encyption key separately (in a key
vault)
Cookies are used to store data about user's preferences or login credentials. There
are generally two kinds of cookies: "persistent" cookies and "session" cookies.
Some of the cookies purposes are: session management, personalization, analytics,
advertising.
Mern stack
❓ 83 What does MERN stand for in the context of web development?
Politica CORS este configurată de serverul backend și specifică care domenii sunt
autorizate să facă cereri către server și sub ce condiții. Acest lucru se face prin
intermediul antetelor HTTP, cum ar fi "Access-Control-Allow-Origin" și altele, care
indică permisiunile pentru resursele respective.
❓ 87 What advantages does a developer have for using bootstrap or material ui?
Using bootstrap or material ui helps developers save time, because they don't have
to write the code from scratch using HTML, JS and CSS. Also, this helps maintain a
consistent user interface across the application. Another advantage is that it is
easy to adapt the interface to different devices and gadgets (tablet, mobile, pc,
etc).