Skip to content

Instantly share code, notes, and snippets.

@AugustoAleGon
Last active December 9, 2017 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AugustoAleGon/e23356cc8bfe0aab68eea87d2d4e77f0 to your computer and use it in GitHub Desktop.
Save AugustoAleGon/e23356cc8bfe0aab68eea87d2d4e77f0 to your computer and use it in GitHub Desktop.
MozioTest
1. Write a function that repeats the String with the following output:
String.prototype.repeatify = function(n){
//Base case
if( n == 1){
return this;
} else{
return (this + this.repeatify(n -1));
}
}
2. Return the file extension or "false" if no extension.
Please explain how would you implement a unit test for this method. What would you test?
function getFileExtension(file) {
let fileArr = file.split(".");
if( fileArr.length > 1 ){
if( fielArr == ""){
return false;
}
return (fileArr[1].toString());
} else {
return false;
}
}
For unit test for the method I will use some importants cases like entryinput = "test.jpg" so the output should be .jpg
Other important example is when you just have this entryinput = ".gitignore" so the output should be .gitignore
If we type something like this entryinput = "somerandomfile" the output should be false.
Another example that might happen is entryinput = "example." the output should be false, because there is an empty extension.
Question 3: Write a simple validation script for a simple form
* - If the required fields are not filled out, do not submit the form
* - If the email is invalid, do not submit the form
* - To validate the email, please call the validateEmail function
* - The form should be able to have more fields added to it and
still work, without changing the JavaScript
- The form doesn't need to post to a specific URL but please comment
inside the code to demonstrate where this would happen.
If it is a simple validation form when you place inside of the specif input with the word "required", basically you have
already solved and there is not needed of Javascript.
Inside the input tag of email what is recommended is to place the type of the input as "email", so you have double validation.
// In case of post there is one more thing to do
function validate(){
return validateEmail(document.myForm.email.value);
}
//For posting I will use a function in the onsubmit based on the result of the validate post at the url or no.
// So no modification to the code.
4: What is variable hoisting? How the new ES6 keywords const and let solve some cases?
Variable hoisting is what Javascript does when it compiles, so basically all the variables at declare first, it doesn't matter
if there are declare at the bottom, they will be moved to the top. In ES6 let and const are created to avoid the use of var
so let basically has the same function as var but the difference is that the scope of the variable is on a block.
In const we have that we cannot reassign a the variable for example:
If we declare a const myValue = 3;
Once we can't change like this: myValue = 4;
That will give us an error. With let you avoid that use of have a variable that
you can access to a variable that is not in the same block scope.
5. What is a “closure” in JavaScript?
and Why is it no longer necessary to wrap your code in a closure with modern JS? Provide examples.
Closure is when a function doesn't have any internal variables and the variables are outside in the father functions.
For example:
function father() {
var a = 1;
function closure() {
console.log(a);
}
closure();
}
father();
As we can see in the example we are using a variable that is outside of the scope of the "closure" function.
When we are using modern JavaScript we will have this code:
function father() {
let a = 1;
function closure() {
console.log(a);
}
closure();
}
father();
In this case the we will have the same behavior. The closure has access to variables in three scopes;
specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables.
so for example:
var globalVar = "xyz";
(function outerFunc(outerArg) {
var outerVar = 'a';
(function innerFunc(innerArg) {
var innerVar = 'b';
console.log(
"outerArg = " + outerArg + "\n" +
"innerArg = " + innerArg + "\n" +
"outerVar = " + outerVar + "\n" +
"innerVar = " + innerVar + "\n" +
"globalVar = " + globalVar);
})(456);
})(123);
6: How does prototypal inheritance differ from classical inheritance?
The main difference between prototypal inheritance is that the classical inheritance is like a blueprint of a class already
defined and the prototypal inheritance is inherit directly from other objects.
In a classical inheritance, instances are typically instantiated via constructor functions with the `new` keyword.
7: Write a code to remove duplicates from an array and return an array of only unique elements.
function removeDuplicate(arr){
var newArr = [];
for(var i = 0; i < arr.length; i++){
if( i == 0){
newArr.push(arr[i]);
}
var foundN = true;
for ( var j = 0; j < newArr.length; j++){
if(newArr[j] == arr[i]){
foundN = false;
}
}
if(foundN){
newArr.push(arr[i]);
}
}
return newArr;
}
8. What means that Functions are a first class citizen in Javascript?
Basically what that means is that Javascript allows to pass functions as a variables.
9. What means that something is Immutable? And what are the advantages of using immutability in data flow?
As the name same is basically that the object is not changing is properties once is defined, you can add more data but you can
change what you wrote in the past. The advantages of that is that you have access to the whole history of data, so it is really
easy to have access to the data.
10. What is a pure function and what are the advantages of using it?
Pure functions always return always returns the same result if the same arguments are passed in.
They function don't modify the input just create another variable.
It does not depend on any state, or data, change during a program’s execution. It must only depend on its input arguments.
The advantage of that does not produce any observable side effects such as network requests,
input and output devices, or data mutation.
They also makes maintaining and refactoring code much easier.
11. In order to make components more reusable which components should carry specific business logic and which ones shouldn’t?
We should separate the component that are just for view with certains functions that are not really so critical,
when I mean critical I mean important features like display from database or generate push notifications.
What I recommend is to structure the project by layers of components of views and with production features.
12. What advantage have the one data flow used by React/Redux (and other flux implementations)
compared with the two way binding flow?
The most important advantage on the React/Redux is when your application is growing up and you have more and more component,
and you want to pass data between those components, in Redux you have an object that has all the data that you need,
so in order to use it you just need to import that data. If when you have to pass between component in Angular you used
a two way binding flow so it is more work if we have a big application.
13: What is a High Order Component?
A Higher Order Component is just a React Component that wraps another one.
14: What are the pros and cons of functional programming vs object-oriented programming?
OOP Pros:
— Objects and methods are very readable and understandable.
— OOP utilizes an imperative style, in which code reads like a straight-forward set of instructions as a computer would read it.
OOP Cons:
— OOP commonly depends upon shareable state. The unfortunate result of so many objects and methods existing
within the same state and being accessed in an entirely undetermined order can lead the pre-discussed concept of “race conditions”.
FP Pros:
— Utilizing pure functions, leads to reliable functions with no side effects that accomplish and return exactly what you expect them to.
— FP utilizes a more declarative style, which focuses more on what to do and less about how it’s being done. This places the emphasis on performance and optimization, leaving the door to refactor without completely reworking your code.
FP Cons:
— Functional programming is a newer paradigm. It’s much easier to find documentation and information on the OOP approach.
— Similar to one of OOP’s strengths, functional programming can lack readability at times. Sometimes functions can become very verbose and become difficult to follow comparatively to the object-oriented style.
15. What does “favor object composition over class inheritance” mean?
Basically is a comparison between this:
Object composition — is an artitecture. The greedy way to break complex problem to smaller and doable ones.
Class inheritance — is a tool. A building block. A saw.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment