<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alexandra Fren</title>
    <description>The latest articles on DEV Community by Alexandra Fren (@alexandrafren).</description>
    <link>https://dev.to/alexandrafren</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F190676%2F8566d70c-2f34-4950-8b3e-cfd7abb0b599.jpg</url>
      <title>DEV Community: Alexandra Fren</title>
      <link>https://dev.to/alexandrafren</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexandrafren"/>
    <language>en</language>
    <item>
      <title>Mastering FizzBuzz</title>
      <dc:creator>Alexandra Fren</dc:creator>
      <pubDate>Wed, 22 Jan 2020 17:10:48 +0000</pubDate>
      <link>https://dev.to/alexandrafren/mastering-fizzbuzz-4k43</link>
      <guid>https://dev.to/alexandrafren/mastering-fizzbuzz-4k43</guid>
      <description>&lt;p&gt;I had heard about "FizzBuzz", of course, but never encountered it in the wild. Recently in a technical interview, I was asked to complete this ubiquitous challenge. I did so in JavaScript, and followed the Red, Green, Refactor pattern to get to my final answer.&lt;/p&gt;

&lt;p&gt;The basics of "FizzBuzz" are as follows:&lt;/p&gt;

&lt;p&gt;-For a provided input, if divisible by 3, output "Fizz"&lt;br&gt;
-For a provided input, if divisible by 5, output "Buzz"&lt;br&gt;
-For a provided input, if divisible by 3 &amp;amp; 5, output "FizzBuzz"&lt;br&gt;
-All else, output the provided input&lt;/p&gt;

&lt;p&gt;The function provided to me in my technical challenge was:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let FizzBuzz = (n) =&amp;gt; { return n }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This allowed for the final of the four tests to be passing from the beginning.&lt;/p&gt;

&lt;p&gt;I started by writing psuedo-code to gather my thoughts about the best way to solve this.  I wrote a conditional statement that evaluated the input based on "divisibility". It's important to note that the conditional for "FizzBuzz" must go first, otherwise the return will end the execution of the function whenever a number is divisible by either 3 or 5 before being able to reach the conditional for both numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let FizzBuzz = (n) =&amp;gt; {
 if (n%3 === 0 &amp;amp;&amp;amp; n%5 === 0) {
    return "FizzBuzz"
 }
 else if (n%3) {
    return "Fizz"
 }
 else if (n%5) {
     return "Buzz"
 }
 else {
    return n
 } 
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This alone is sufficient enough to pass the requirements of FizzBuzz, but the number of lines of code can be reduced. Because a return ends the execution of a function, we can remove the else if statements and have 3 if statements. Additionally, because of the simplicity of these conditional statements, we can place all of our code on one line and remove the brackets. This reduces our FizzBuzz function down to four lines of code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let FizzBuzz = (n) =&amp;gt; {
 if (n%3 === 0 &amp;amp;&amp;amp; n%5 === 0) return "FizzBuzz"
 if (n%3) return "Fizz"
 if (n%5) return "Buzz"
 else return n
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;While it might seem tempting to just print out the final answer in an interview, going through these steps shows your ability to think logically and how well you can communicate, by talking through the steps you are taking with your interviewer. &lt;/p&gt;

</description>
      <category>fizzbuzz</category>
      <category>interview</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>A JavaScript Fundamentals Cheat Sheet: Scope, Context, and “this”</title>
      <dc:creator>Alexandra Fren</dc:creator>
      <pubDate>Fri, 05 Jul 2019 03:20:13 +0000</pubDate>
      <link>https://dev.to/alexandrafren/a-javascript-fundamentals-cheat-sheet-scope-context-and-this-28ai</link>
      <guid>https://dev.to/alexandrafren/a-javascript-fundamentals-cheat-sheet-scope-context-and-this-28ai</guid>
      <description>&lt;p&gt;Scope&lt;/p&gt;

&lt;p&gt;Scope refers to where a variable can be accessed within a program. Some variables can be accessed from anywhere within a program (global scope), while others have a more limited context (function and block scopes).&lt;/p&gt;

&lt;p&gt;When we are not in a function or a block (code grouped between two curly braces {}), we are in the global scope. Any variable declared here can be accessed anywhere else within the global scope, but also within a function or a block.&lt;/p&gt;

&lt;p&gt;Variables can be declared in function scope, which exists within a function. These variables are accessible to any code within that function, including additional blocks, but they are not accessible within the global scope.&lt;/p&gt;

&lt;p&gt;//This variable is declared in the global scope&lt;br&gt;
let user = "Alex";&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greetUser(){
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;//This variable is declared in the function scope&lt;br&gt;
       let greeting = "Hello, ";&lt;/p&gt;

&lt;p&gt;//This return can access both variables declared within its &lt;br&gt;
scope(function), as well as global scope variables&lt;br&gt;
       return greeting + user;&lt;br&gt;
    }&lt;/p&gt;

&lt;p&gt;Block scope is observed by let &amp;amp; const (the variable declarations introduced with ES6), but not by var. As such, var shouldn’t be used when declaring variables in block scope (or in most situations). Variables declared (with let or const) within block scope will only be accessible within that scope, and not within any outer scopes. Block scopes however have access to all variables declared in the global scope, as well as within any containing functions.&lt;/p&gt;

&lt;p&gt;Variables within a block will act the same as they would if defined within a function, meaning they can not be re-declared, and const variables can not be redefined, within that block. However, because these variables are only scoped within this block, a variable of the same name could be declared within the containing function without necessarily causing problems.&lt;br&gt;
Context&lt;/p&gt;

&lt;p&gt;Context refers to an object, and it is relevant when executing a function. Objects have properties and methods that can be accessed using ‘this’, and knowing the context of a function is important so that we understand what the value of ‘this’ will be after executing a function.&lt;/p&gt;

&lt;p&gt;Generally, the context of a function will refer to the object it is being called on. This is true for functions that are nested within other functions on an object, and for constructor functions that are used when instantiating objects. When a function is not called on an object, the context is the global, meaning ‘this’ will refer to the window object.&lt;br&gt;
“this”&lt;/p&gt;

&lt;p&gt;Context is important because it highlights the object that will be passed to the value of the ‘this’ property that we can then use when executing a function. This allows us to access the values of the object within the function, letting us write functions that may be reused by multiple objects, keeping our code DRY.&lt;/p&gt;

&lt;p&gt;When we have an object with functions stored as an attribute value, ‘this’ is passed implicitly to the function, because the function is defined as part of the objects declaration, and so the function will always know how it is being invoked. In the example below, calling user.greeting(), implicitly sets the value of ‘this’ to the user (the object the function is being called on).&lt;/p&gt;

&lt;p&gt;const user = {&lt;br&gt;
   name: "Alex",&lt;br&gt;
   age: 26,&lt;br&gt;
//'this' value will be passed to the function implicitly as it is &lt;br&gt;
defined as part of the object variable&lt;br&gt;
   greeting(){&lt;br&gt;
       "My name is ${this.name}, and I am ${this.age} years old.";&lt;br&gt;
   }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Bind, call, and apply are built in functions, introduced in ES5, that allow you to pass a parameter of the context object that should be used for ‘this’. They are helpful when calling a function that is defined within the global scope on an object, or when you are using an built-in JavaScript method (such as reduce, filter, map, etc.) and need to explicitly state the context so that the function does not return a TypeError.&lt;/p&gt;

&lt;p&gt;function greeting() {&lt;br&gt;
    console.log(&lt;code&gt;Hello, ${this.name}!&lt;/code&gt;);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;const user = { &lt;br&gt;
   name: 'Alex' &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;//Passing the object through call ensures that the greeting function will have access to the correct object through 'this' &lt;br&gt;
greet.call(user);&lt;/p&gt;

&lt;p&gt;Constructors create an object and then use that newly instantiated object as the ‘this’ value, allowing it to then set values to attributes.&lt;/p&gt;

&lt;p&gt;class Game {&lt;br&gt;
//The context within a constructor is the object instantiated, allowing &lt;br&gt;
us to access the new object with 'this', and set its attribute values&lt;br&gt;
    constructor(id, name, release_year, consoles, review = ""){&lt;br&gt;
        this.id = id;&lt;br&gt;
        this.name = name;&lt;br&gt;
        this.release_year = release_year;&lt;br&gt;
        this.consoles = consoles;&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Arrow functions, introduced in ES6, do not have an implicit “this”, and can be used in places where we might otherwise have needed a bind, call, or apply method. They will look at their containing function or parent to determine the value of ‘this’.&lt;/p&gt;

&lt;p&gt;The final way that ‘this’ can be defined is when a function is written in the global scope but uses ‘this’ and does not make use of bind, call, or apply when invoking the function. In this instance, the value of ‘this’ will be the window object, and will return as undefined. &lt;/p&gt;

&lt;p&gt;**This article was originally posted on my blog at &lt;a href="http://alexandrafren.com/2019/02/28/a-javascript-fundamentals-cheat-sheet-scope-context-and-this/"&gt;http://alexandrafren.com/2019/02/28/a-javascript-fundamentals-cheat-sheet-scope-context-and-this/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
