<?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>Forem: Ben</title>
    <description>The latest articles on Forem by Ben (@bencodes).</description>
    <link>https://forem.com/bencodes</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%2F1222161%2Fb71a95db-bc1d-4289-a922-3aa363fd2493.png</url>
      <title>Forem: Ben</title>
      <link>https://forem.com/bencodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bencodes"/>
    <language>en</language>
    <item>
      <title>Code Challenges pt. 2</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Wed, 29 Nov 2023 23:29:44 +0000</pubDate>
      <link>https://forem.com/bencodes/code-challenges-pt-2-4kjm</link>
      <guid>https://forem.com/bencodes/code-challenges-pt-2-4kjm</guid>
      <description>&lt;p&gt;Part 1 of this series is &lt;a href="https://dev.to/bencodes/code-challenges-pt-1-3df7"&gt;Reverse a String&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Next up, we're working with Palindromes
&lt;/h5&gt;

&lt;p&gt;Prompt:&lt;br&gt;
You are given a string. Check to see if it is a palindrome. If it is return true, if it is not return false.&lt;/p&gt;

&lt;p&gt;Include all characters (spaces, punctuation, numbers, etc.) in determining if the string is a palindrome.&lt;/p&gt;

&lt;p&gt;Note: Palindromes are words that are the same when spelled forwards as backwards, e.g. racecar.&lt;/p&gt;

&lt;p&gt;Example outcomes:&lt;br&gt;
isPalindrome('racecar') === true&lt;br&gt;
isPalindrome('animal') === false&lt;br&gt;
isPalindrome('mom') === true&lt;/p&gt;

&lt;p&gt;Code:&lt;br&gt;
&lt;em&gt;Note: all we're doing is checking to see if the string is the same forwards as backwards...&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isPalindrome(string){
    const reversed = string.split('').reverse().join('')
    return reversed === string
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Why does this work?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First we just make a reversed copy of the string.&lt;/p&gt;

&lt;p&gt;We could use the for-loop version (&lt;a href="https://dev.to/bencodes/code-challenges-pt-1-3df7"&gt;Version 2 of Reverse a String&lt;/a&gt;), but it's not the &lt;em&gt;only&lt;/em&gt; thing we're doing so we might as well just use the built-in methods.&lt;/p&gt;

&lt;p&gt;From there, since all we're doing is comparing the string being passed in to the reversed copy, we return the boolean value from the expression &lt;code&gt;reversed === string&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Code Challenges pt. 1</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Wed, 29 Nov 2023 21:52:45 +0000</pubDate>
      <link>https://forem.com/bencodes/code-challenges-pt-1-3df7</link>
      <guid>https://forem.com/bencodes/code-challenges-pt-1-3df7</guid>
      <description>&lt;p&gt;Going from beginner on up, this will become a long series of posts covering coding challenge questions and answers.&lt;/p&gt;

&lt;p&gt;The real goal here is to give myself some extra practice as well as build a reference to many different challenge questions.&lt;/p&gt;

&lt;p&gt;Primary languages = JavaScript/TypeScript, may include some Java as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First up, Reverse a String&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prompt:&lt;br&gt;
Given a string, return a &lt;em&gt;new&lt;/em&gt; string with the characters in reversed order.&lt;/p&gt;

&lt;p&gt;Example outcomes:&lt;br&gt;
reverseString('orange') === 'egnaro'&lt;br&gt;
reverseString('hello world') === 'dlrow olleh'&lt;br&gt;
reverseString('How About That?') === '?tahT tuobA woH'&lt;/p&gt;

&lt;p&gt;Code:&lt;/p&gt;

&lt;h5&gt;
  
  
  Version 1 (using built-in string methods)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reverseString(string) {
    return string.split('').reverse.join('')
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Why does this work?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, our function takes in a string and uses the &lt;code&gt;split()&lt;/code&gt; method on it.&lt;/p&gt;

&lt;p&gt;The first argument you give &lt;code&gt;split()&lt;/code&gt; is the separator - the character in the string that you want the string to be split on.&lt;/p&gt;

&lt;p&gt;By giving it an empty string (note: '' does not have a space between it), you're telling it to split the string at each and every character.&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;split()&lt;/code&gt; returns an array of strings, so if we split "Hello" for example, it would return &lt;code&gt;["H", "e", "l", "l", "o"]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And that's how we get to our next step - the .reverse() method.&lt;/p&gt;

&lt;p&gt;.reverse() is not a string method, but an array method, which is why we can only work with it after .split() returns our array.&lt;/p&gt;

&lt;p&gt;Suffice it to say, that does most of the work on this problem.&lt;/p&gt;

&lt;p&gt;The last step is another array method - .join() - which we essentially can think of as doing the opposite of .split() here.&lt;/p&gt;

&lt;p&gt;The .join('') method concatenates the strings in the array when given an empty string, and returns a string from the array method as well.&lt;/p&gt;

&lt;h5&gt;
  
  
  Version 2 (for-loop version)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reverseString(string) {
    let revString = ''
    for (let i = 0; i &amp;lt; string.length; i++){
        revString = string[i] + revString
    }
    return revString
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Why does this work?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the for-loop version, first we declare a new, empty string.&lt;/p&gt;

&lt;p&gt;The idea here is that we have this empty string, and then as we loop through each character of our given string, we add the current character to the left of reversed string (revString).&lt;/p&gt;

&lt;p&gt;Now let's pretend our string is "Hello".&lt;/p&gt;

&lt;p&gt;The first time through the loop, revString is empty.&lt;/p&gt;

&lt;p&gt;We enter our for loop, and we're telling it to loop through the string from left to right.&lt;/p&gt;

&lt;p&gt;Since revString is empty, so we set it equal to the first character of the string (&lt;code&gt;string[i]&lt;/code&gt; - because i starts at index 0), &lt;em&gt;plus&lt;/em&gt; the current revString value (which is empty).&lt;/p&gt;

&lt;p&gt;This sets revString to 'H' as we begin our second iteration through the loop.&lt;/p&gt;

&lt;p&gt;Next time through, revString = 'H', string[i] = 'e', so revString = 'e' + 'H', which becomes 'eH'.&lt;/p&gt;

&lt;p&gt;To illustrate one last loop now revString = 'eH', string[i] 'l', so revString = 'l' + 'eH', which becomes 'leH'.&lt;/p&gt;

&lt;p&gt;And you get the rest :)&lt;/p&gt;

&lt;p&gt;Next up, Palindromes...&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
