DEV Community: Meg The latest articles on DEV Community by Meg (@megdiv). https://dev.to/megdiv 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%2F981491%2Fd8e86c54-8cc0-4c7b-ad12-8723aed490c4.jpeg DEV Community: Meg https://dev.to/megdiv en JavaScript Ternary Operator Meg Sat, 08 Nov 2025 21:04:24 +0000 https://dev.to/megdiv/javascript-ternary-operator-kmo https://dev.to/megdiv/javascript-ternary-operator-kmo <p>One of my favorite parts of the JavaScript language is the JavaScript Ternary Operator.</p> <p>At heart, it’s a succinct, clean way of writing an if/else statement.</p> <p>You start by asking a question, <br> "Is this item a circle?"</p> <p>If the item is a circle, it takes path one<br> If the item is not a circle, it takes path two</p> <p>You would write it in JavaScript like this:</p> <p>circle ? true : false</p> <p>A ternary operator can also be stacked or ‘nested’. </p> <p>In this example the item we’ll use is a triangle.</p> <p>You would use the same first condition:</p> <p>circle ? true : _____</p> <p>And replace the false condition with a second ternary:</p> <p>triangle ? true : false</p> <p>So a nested ternary would be:</p> <p>circle ? true : triangle ? true : false</p> <p>You can stack as many conditions as you like and adjust the formatting to visually appear like a switch statement:</p> <p>circle ? <br> true : triangle ? <br> true : square ? <br> true : rectangle ?</p> <p>You are also able to return with a ternary operator by listing "return" before your condition:</p> <p>return circle ? true : false </p> <p>Unpacking this statement, it should read:<br> Is the item a circle? <br> If the item is a circle, return true<br> If the item is not a circle, return false</p> <p>By using "return" with a ternary you must always return from either condition. It's not able to be used in instances where you only want to return on true, but not false, and vice versa.</p> <p>All in all, I hope this helped understand the JavaScript Ternary Operator a bit better and why I find it so visually clean and fun to use.</p> Avocado Meg Tue, 14 Mar 2023 21:11:32 +0000 https://dev.to/megdiv/avocado-3im3 https://dev.to/megdiv/avocado-3im3 <p>Concept:</p> <p>Our group decided early on that we liked the idea of doing a food related app, an ordering, back of house, reporting tool that could be used by a customer to order food, but also by the restaurant to get the food ready, and the owner to review revenue and edit menu information. Our idea was to create an app similar to Toast, so we decided to call our app "Avocado". </p> <p>Team Roles:</p> <p>Corey set up our Supabase schema, our initial routes and multiple table backend routes, connecting our backend and frontend servers and creating the reporting system for the back-of-house operation page.</p> <p>Peter designed our brand identity and styling, creating the restaurant logos and layout with Adobe Illustrator, and implementing the menu, cart, and order confirmation pages as well as their frontend Supabase routes. </p> <p>Jaye was our project manager and styling TailwindCSS specialist, leading our multiple daily standups, acting as our instructor liaison, creating our mockup flow chart in Figma and database schema in DB designer, as well as managing our Github project tracking page, setting up our Supabase storage, and deploying our project front and back ends on Render. </p> <p>Meg designed the login, signup, and dashboard logic as well as the reducers slice for our Redux state, implementing state persist, our front-end routes and state logic queries, and creating our reporting in Plotly to showcase database statistics.</p> <p>Blockers:</p> <p>Although choosing to use Supabase allowed us to easily authenticate and grab our token to set into state, we later discovered Supabase automatically set user emails to lowercase when they were put in the authentication table, which caused an issue when we tried to query them. Other blockers were how Supabase handled image storage, and its timing delay of returning data that generated the need to conditionally render many of our designs.</p> <p>Final Thoughts:</p> <p>In total, our project included React, Redux, JavaScript, Supabase, Plotly, Toastify, TailwindCSS, and State Persist - many of which were used for the first time by at least one person in our group. Overall, our project definitely pushed us to learn new tools and technologies and we are proud to present our food service app, Avocado!</p> <p><a href="proxy.php?url=https://github.com/Lasseignejk/Avocado-frontend" rel="noopener noreferrer">Project Repo - Frontend</a><br> <a href="proxy.php?url=https://github.com/Lasseignejk/Avocado-backend" rel="noopener noreferrer">Project Repo - Backend</a><br> <a href="proxy.php?url=https://avocado-frontend.onrender.com/" rel="noopener noreferrer">Project Site</a><br> <a href="proxy.php?url=https://youtu.be/wsLZCA090U4" rel="noopener noreferrer">Project Walkthrough</a></p> react redux javascript tailwindcss Dalle Meg Wed, 22 Feb 2023 16:54:47 +0000 https://dev.to/megdiv/dalle-14ca https://dev.to/megdiv/dalle-14ca <p>I started out on my React project with a few ideas in mind. I knew I wanted to create an art showroom with art created by <a href="proxy.php?url=https://openai.com/dall-e-2/" rel="noopener noreferrer">DALL·E</a> so after narrowing down my scope, I decided to not only use the required React and Redux to create my project, but also Tailwind and TypeScript.</p> <p>I pushed a bit further and decided to use Jira to organize myself and Figma to map my flow and edit images, and because CSS is my least favorite part of programming, similar to my frontend project I decided to do it first.</p> <p>Once the project was officially given out I researched up and coming 2023 colors and happened to find a picture I responded to:</p> <p><a href="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0xv32rq2rrshgsvurno.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0xv32rq2rrshgsvurno.png" alt="Living room" width="800" height="463"></a></p> <p>From there I visited Canva to create my 4-pan color palette, while still having a few extras I liked left over:</p> <p><a href="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj301rwcjmjohkjsjh9u5.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj301rwcjmjohkjsjh9u5.png" alt="Color Palette" width="800" height="314"></a></p> <p>That evening I set up my project, creating a base structure and installing everything I planned to use. The next morning on our first day of work I separated out all the aspects of the site into separate JIRA tickets. This helped me stay organized and cut down on the bevy of open browser windows I’m known for.</p> <p>Although I created my navbar first from several different tutorials, I quickly discovered my fancy navbar did not play well with having a cart amount bubble attached to it. Not having a full tutorial for what I wanted to do became a blocker, so after creating my background image via DALL*E and struggling to have it visually appear on the front page as I needed, I started my search for a modern tailwind react e-commerce design while in the meantime creating all the art shown on the project page via DALL*E.</p> <p><a href="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzyvz2e72rqjwvdnde5f.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzyvz2e72rqjwvdnde5f.png" alt="DALL*E Art" width="526" height="536"></a></p> <p>After finding a react tailwind tutorial that I liked (which unfortunately did not use Redux or TypeScript) I finally started heading towards having a design with pieces which worked well together.</p> <p>Because the tutorial I'd chosen used Context and I was using Redux, I actually got my logic working far ahead of finishing the styling since I used state to hold my cart, data, product information, and whether my sidebar cart was open or not, and used dispatch to call for the data with the reducer functions to add/remove/clear, etc. </p> <p>I also ended up being able to cut numerous "pages" after creating a Layout component, which held my header, sidebar, and footer and let me sandwich any applicable children content to create the “page” I needed. </p> <p>Because I'd chosen to use TypeScript I also created interfaces I kept in the slice, and made sure all my functions were typed. Since I'm used to writing in C++, typing doesn't feel odd to me, however Typescript is different enough that it made it interesting.</p> <p>Overall, I am extremely proud of my final product, and as the project prompt requested, it is "the prettiest application [ I ] have ever made".</p> <p><a href="proxy.php?url=https://youtu.be/6qb6IrObv-Q" rel="noopener noreferrer">Video Walkthrough</a><br> <a href="proxy.php?url=https://63f6c8993b9f8f00088fa524--wonderful-moonbeam-71d581.netlify.app" rel="noopener noreferrer">Project Site</a><br> <a href="proxy.php?url=https://github.com/Meg-Div/React-Project" rel="noopener noreferrer">Project Repo</a></p> react webdev typescript javascript ~~~~~~~~VOTE!~~~~~~~ Meg Sat, 28 Jan 2023 18:05:43 +0000 https://dev.to/megdiv/vote-58eb https://dev.to/megdiv/vote-58eb <p><a href="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbooh2u2brr78d3abvd4i.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbooh2u2brr78d3abvd4i.png" alt="left" width="221" height="46"></a></p> <p>We started out with our backend project by deciding we would choose an overarching idea that we could create under. Peter liked the idea of creating a voting site/app where you could cast your vote for an elected official and I liked the idea of putting a silly spin on it, noting that there were towns in the US with a mayor who was a dog.</p> <p>From there we traced out the scope, detailing all the pages we’d like to create and the information we’d like populated. I noted down our tables and Peter created our schema with DB Designer. Once our idea was approved, Peter chose our site colors and created site mock ups with Adobe Illustrator Xd. These mock ups were integral in helping to visualize our pages throughout the project and from there I began creating our database with sequelize and Elephant sql, while Peter created our EJS files once I’d outlined the routes.<br> <br><br> <a href="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54g51btynmnfr73jo8g2.jpg" class="article-body-image-wrapper"><img src="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54g51btynmnfr73jo8g2.jpg" alt="mock ups" width="379" height="270"></a><br> <br><br> For much of the project we worked in tandem, making decisions on logic and design collaboratively, pooling resources via Zoom when we needed to make changes to EJS and/or new routes. </p> <p>As our deadline neared we agreed to focus on CRUD and the project requirements such as bcrypt, authentication, and security - cutting extra pages and ideas that we liked but that could not be executed in the time allowed.</p> <p>Given more time, we likely would have created a fourth table to manage votes, added in complexity such as the admin drop downs only containing positions already included within the table, disabling buttons, and adding in a printable “I voted” sticker. However overall are pleased with the final product, our voting app, Vote!</p> <p><a href="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fig5gmuhkv8p8vin8jg6e.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fig5gmuhkv8p8vin8jg6e.png" alt="right" width="221" height="46"></a></p> <center>[Project Repo](https://github.com/Meg-Div/BackendProject) [Peter's Github](https://github.com/peterriesing) &amp; [Meg's Github](https://github.com/Meg-Div)</center> showdev Hash Maps Meg Thu, 19 Jan 2023 23:14:31 +0000 https://dev.to/megdiv/hash-maps-o71 https://dev.to/megdiv/hash-maps-o71 <p>It’s not really a secret that I love using hash maps, and one of my goals this week was to teach myself how to use them in Python.</p> <p>Essentially, a hash map is a data structure has key/value pairs. In both Javascript and Python, to create a hash map you would create an empty object like so:<br> <code>obj = {}</code></p> <p>In JavaScript to check if your object contains an element you would say:<br> <code>if (obj[elem])</code></p> <p>But in Python, you have to check for what’s not there first, so it would be:<br> <code>if num not in obj:</code></p> <p>In both languages you increment <code>obj[elem]</code>, marking it <code>= 1</code> if it wasn’t there before, or <code>+=1</code> if it was.</p> <p>This creates an object which holds a key that corresponds to your array or string element, and a value that is equal to the number of times that value occurs. </p> <p>When I came upon today’s algorithm exercise: “Given an array of integers, where all elements but one occur twice, find the unique element...” I knew immediately I would use a hash map.</p> <p>To me, any algorithm that questions instances for unknown multiple elements = hash map. I love them because it’s a simple piece of code that’s easy to remember under pressure and can be utilized to solve many different algorithms.</p> <p>Once I created my hash map like the above, I looked into ways to iterate over my obj in a one liner and came up with next(). Next() returns the next item of an iterator and when given an if statement made it so I could return the unique (single) element that had a value of 1:<br> <code>return next(num for num in a if obj[num] == 1)</code></p> <p>My final code is below. I created an object, iterated through the input array, and added in a new key/value pair or updated the value in my object. Then I iterated through it again to return the key for which the value was equal to 1:</p> <p><code>    obj = {}<br>     for num in a:<br>         if num not in obj:<br>             obj[num] = 1<br>         else:<br>             obj[num] += 1      <br>     return next(num for num in a if obj[num] == 1)</code></p> devmeme watercooler Plus Minus Meg Wed, 18 Jan 2023 22:47:59 +0000 https://dev.to/megdiv/plus-minus-469c https://dev.to/megdiv/plus-minus-469c <p>Although I feel more comfortable coding in C++ and JavaScript, I've been brushing up on my Python for interview practice. The below is a walk though of how I solved the Plus Minus problem, then refactored my original answer.</p> <p>The problem:<br> "Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal."</p> <p>How I solved it (pseudo code):</p> <p>Input: Array<br> Output: Decimal</p> <p>What I'll need:</p> <ul> <li>Because our input is an array I will either need a loop or a method that iterates through that loop.</li> <li>I’ll have to divide by the total number of items in the array, so I'll need the array length. </li> <li>Finally I'll need to keep a tally of how many elements are in each bucket. </li> </ul> <p>I’m sure I could do this with a filter, but I’m going to smash and grab it to start so I'm going to use a for loop, use len() to grab the array length and I'll create buckets for negatives, positives, and zeros.</p> <p>From there I'll use several if statements to check to see if the item falls under certain conditions, and if it does, the corresponding bucket will be incremented. All three buckets will then be printed divided by the length of the original array.</p> <p>The code:<br> <code>neg = 0<br> pos = 0<br> zero = 0<br> length = len(arr)<br> for item in arr:<br> if item &lt; 0:<br> neg+=1<br> if item == 0:<br> zero+=1<br> if item &gt; 0:<br> pos+=1<br> print(pos/length)<br> print(neg/length)<br> print(zero/length)</code></p> <p>Refactoring:</p> <p>Once I pressed “run code” and verified my code worked I went back and researched how to use filter for Python. </p> <p>Looking up filter I found I could use it like so to check to see if the item (x) is larger than 0 and thus positive:<br> <code>filter(lambda x: x &gt; 0, arr)</code></p> <p>The filter method returns an array so I need to chain it with list to print it out: <code>list(filter(lambda x: x &gt; 0, arr))</code></p> <p>From there I can chain it to len() to get the length or size of that array: <br> <code>len(list(filter(lambda x: x &gt; 0, arr)))</code></p> <p>The length will directly correlate to the number of items that are positive, negative, or zero. </p> <p>From there I could divide the length of my filtered array by len(arr), the size of the original array, to print out the decimal value of each fraction. </p> <p>My final code:</p> <p><code>print(len(list(filter(lambda x: x &gt; 0, arr)))/len(arr))<br>  print(len(list(filter(lambda x: x &lt; 0, arr)))/len(arr))<br>  print(len(list(filter(lambda x: x == 0, arr)))/len(arr))</code></p> discuss It's A Circus Meg Fri, 06 Jan 2023 15:27:17 +0000 https://dev.to/megdiv/its-a-circus-3jm8 https://dev.to/megdiv/its-a-circus-3jm8 <p>This project was unusual in that I decided I would style it first and then write the logic for it.</p> <p>I began by walking through a Tailwind course over the holiday break and found I enjoyed using their shortcuts to make mobile responsive elements. Having everything on one page so I could visualize what it would look like as I created it went a long way towards preventing any frustration with CSS.</p> <p>Once I had a working page I could pull bits and pieces from, I went back to my approved idea. I’d chosen several APIs that didn’t require an API key and settled on a simple concept I knew I could execute after the APIs brought to mind a big top circus “guess your age/weight/name” game.</p> <p>Using the Tailwind course as a base allowed me to show/hide elements and update the class name to change the styling instead of hopping from html to html page when I wanted something changed. In the end, the most challenging part of the project was the creation of two side by side buttons with links in JavaScript, and even though they don’t do exactly what I’d envisioned, I spent so much time on them I reworked my idea just so could keep them.</p> <p>Although I ended up with seven HTML pages (two which gave fun facts from a number), after uploading to GitHub an odd bug showed up where the innterText didn't update on those two pages. It would be visible through LiveServer, but not Replit or Pages so I ended up pivoting and removing both.</p> <p>Since all my code was based on the same general structure, I just shuffled the fortune teller section from one function to another, removed the two pages, and it worked on the first try. </p> <p>Looking back, choosing to use Tailwind, key-free APIs, and an un-obscure theme at the beginning saved me time and headaches with styling and logic. The more projects I do the more I understand how important it is to spend time on editing your idea, then confirming your data, before ever sitting down to code.</p> <p>The code: <br> <a href="proxy.php?url=https://github.com/Meg-Div/ItsACircus" rel="noopener noreferrer">https://github.com/Meg-Div/ItsACircus</a></p> <p>Play it:<br> <a href="proxy.php?url=https://meg-div.github.io/ItsACircus" rel="noopener noreferrer">https://meg-div.github.io/ItsACircus</a></p> discuss career motivation productivity Magic 8 Ball Meg Tue, 27 Dec 2022 17:25:09 +0000 https://dev.to/megdiv/magic-8-ball-2p2h https://dev.to/megdiv/magic-8-ball-2p2h <p>Magic 8 Ball originally was a class project from my most recent Summer course, however the professor required that we not post that code anywhere, so I’ve been refactoring it so it doesn’t match what we originally did, while staying true to the Magic 8 Ball idea.</p> <p>I started out by searching for the possible options for a real Magic 8 Ball, cleared out the array and added them in. From there I updated the class private variables to remove unneeded variables, and set the default constructor to be empty.</p> <p>I’ve been working on sticking to the DRY method, “Don’t Repeat Yourself”, so I moved the “checking” code (which evaluates the input by capitalizing and comparing it) into its own function. I also updated the main driver code so that the four other functions call each other, removing the need for a while loop. After the first shake is completed, the three other functions bounce from one to another while the user wants to continue shaking the Magic 8 Ball. Once the user inputs N, the driver code ends.</p> <p>Wrapping up editing the small project by updating my Class UML, I realized that although I haven’t been coding in C++ since moving on from my most recent college Computer Science course, it really is like riding a bike and comes right back to me when I hop back into it. All in all, I had fun with the original project and enjoyed refactoring this very small game so I hope you enjoy!</p> <p>Play it: <a href="proxy.php?url=https://replit.com/@Meg-Div/Magic-8-Ball#main.cpp" rel="noopener noreferrer">https://replit.com/@Meg-Div/Magic-8-Ball#main.cpp</a></p> <p>The Code: <a href="proxy.php?url=https://github.com/Meg-Div/DigitalCraftsProjects/tree/main/Magic8Ball" rel="noopener noreferrer">https://github.com/Meg-Div/DigitalCraftsProjects/tree/main/Magic8Ball</a></p> css frontend webdev showdev A question of FizzBuzz Meg Wed, 21 Dec 2022 04:06:54 +0000 https://dev.to/megdiv/fizzbuzz-1c8h https://dev.to/megdiv/fizzbuzz-1c8h <p>Today in class we were talking about the FizzBuzz question and if it can be solved with no conditionals or for loops.</p> <p>So in C++ there’s something called <a href="proxy.php?url=https://en.cppreference.com/w/cpp/language/goto" rel="noopener noreferrer">goto</a>, which acts like a loop without actually being one. If you look at the documentation for it, Dijkstra (of shortest path algorithm) wrote an essay about it titled <a href="proxy.php?url=http://david.tribble.com/text/goto.html" rel="noopener noreferrer">“Goto Considered Harmful”</a>. Which should really tell you enough about why you shouldn’t use it. But for funsies I did:</p> <p><a href="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd13wlku444rvy9udj12a.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd13wlku444rvy9udj12a.png" alt="C++ with ternary" width="776" height="648"></a></p> <p>To be honest, most people start solving this problem with an if/else statement and a loop. So switching to JavaScript I did just that.</p> <p><a href="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgterjfq4gakpnyk81occ.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgterjfq4gakpnyk81occ.png" alt="Image description" width="552" height="570"></a></p> <p>From there I decided to refractor it to a ternary because I love me some nested ternary. It makes most people’s eyes cross, but there’s something about nested ternary that when formatted correctly, looks so effortlessly clean.</p> <p><a href="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1m8yvopyi6vko2xqg2i.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1m8yvopyi6vko2xqg2i.png" alt="Image description" width="530" height="584"></a></p> <p>Moving on, I went ahead and researched what other people did to try and solve the algorithm without if/else statements. Quite honestly in my previous FizzBuzz solves I'd never gone that far down the rabbit hole so I started off with a <a href="proxy.php?url=https://micheleriva.medium.com/about-coding-the-fizzbuzz-interview-question-9bcd08d9dfe5" rel="noopener noreferrer">Medium article by Michele Riva</a>, and ended up at a <a href="proxy.php?url=https://dev.to/nombrekeff/challenge-fizzbuzz-without-if-else-33c8">Dev.to article by Keff</a>. </p> <p>Looking through the examples opened my mind to how I might get around needing if/else or || and &amp;&amp; to check each element. I blatantly stole having three checks in separate functions, but then researched using replace() with RegEx. And although it seems like you can do it, I wasn’t able to push it through, mores the pity.</p> <p>From there I switched to finding the index so I could see about replacing the element. And that was when I stumbled on the MDN documentation for findIndex();</p> <p><a href="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figi8ybtncxsfdxwrue7o.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figi8ybtncxsfdxwrue7o.png" alt="MDN defintion page" width="800" height="206"></a></p> <p>I had never used findIndex() with a function inside of it before. So I paired it with splice() and tested it out - and it worked.</p> <p><a href="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbv3o7bs9rubf4im38e0l.png" class="article-body-image-wrapper"><img src="proxy.php?url=https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbv3o7bs9rubf4im38e0l.png" alt="JS for loop no conditionals" width="706" height="682"></a></p> <p>My solution:</p> <ul> <li>Three separate functions checking to see if the element is divisible by 3, 5, or 15. </li> <li>Creates an array filled with ascending elements 0 through N.</li> <li>Removes the zero element with shift().</li> <li>Runs a for loop to find the index of each element that is divisible by 3, 5, or 15 using findIndex(). </li> <li>Uses splice() to remove it and replace each element with “FizzBuzz”, “Fizz” or “Buzz”.</li> </ul> <p>After confirming by comparing it against my other previous solves I was left with the same feeling I get when I do something totally random to solve a problem. “It works!... But why would you ever do it that way.”</p> <p>So yes, FizzBuzz with no if/else. But why would you ever do it that way.</p> watercooler Forever 21 Meg Fri, 16 Dec 2022 22:50:03 +0000 https://dev.to/megdiv/forever-21-268m https://dev.to/megdiv/forever-21-268m <p>This week in class we worked through creating a JavaScript Blackjack game.</p> <p>Given starter code, our goal was to create logic that followed the basic rules of the game Blackjack: </p> <ul> <li>Player or Dealer wins at 21 or whomever is closer without going over 21.</li> <li>Dealer stands after 17.</li> <li>An Ace (11) can also be worth 1.</li> <li>Cards aren’t duplicated unless multiple decks are used.</li> </ul> <p>I set some stretch goals for myself to disable buttons that weren’t needed, such as disabling Hit and Stand until Deal was clicked, disabling Deal after one click, and disabling Hit after Stand was clicked.</p> <p>I also wanted to list the total, show a “You Won/Lost” message, and flip the Dealer’s cards over at the end of the game.</p> <p>I started out feeling comfortable dropping into someone else’s code and began just console logging the object to see what was being produced. From there I chose a simple shuffle algorithm to shuffle the created deck and and moved on to creating a function that would create and append the card element, making one for the Player and one for the Dealer, deleting any dealt cards from the deck after they were appended.</p> <p>Moving on, I created the logic for the three existing buttons, learning how to disable and re-enable them, and then added in another button right below them to refresh the page and start a new game. </p> <p>From there I worked on the logic that was run by clicking the buttons. On Deal I would call both element creation functions twice. On Hit, I would call both functions once and on Stand I would call only the Dealer’s function. </p> <p>Additionally, because I wanted to turn off certain buttons in certain card total situations and needed to add in logic for if the Dealer reached 17, I also needed to create a counting function to update the Player and Dealer totals after each click. </p> <p>My counting function tracked the current Player/Dealer total by reducing the two global arrays that each held how much each card in their hand was worth and from there I created four if/else sections to handle the different paths. One handled anyone at 21, one recursively handled going over 21 but having an ace, one handled if either the Player or Dealer went bust, and the last one handled if the Dealer hit 17 and the Player hit Stand. </p> <p>Consequently, each resulting section would update the inner text to include the final Player total and either a win or lost statement. They also disabled the buttons that were not needed in each situation and once the game was over, all the Dealer’s cards were flipped over and their total was revealed. </p> <p>This was done through a suggestion from our cohort teacher, letting the CSS class content style supersede any src set to the element until that class's style was removed, revealing its underlying src.</p> <p>Overall, I had fun designing the logic for this game and working through accounting for each game path possibility.</p> <p>If you'd like to play it:<br> <a href="proxy.php?url=https://meg-div.github.io/blackjack/" rel="noopener noreferrer">https://meg-div.github.io/blackjack/</a></p> <p>Here's the code:<br> <a href="proxy.php?url=https://github.com/Meg-Div/blackjack" rel="noopener noreferrer">https://github.com/Meg-Div/blackjack</a></p> javascript beginners showdev You Can Count On Me Meg Sat, 10 Dec 2022 01:28:03 +0000 https://dev.to/megdiv/you-can-count-on-me-534j https://dev.to/megdiv/you-can-count-on-me-534j <p>Yesterday during class we were set a task to create a calculator.</p> <p>It’s been about 2 years since I did anything with event listeners but I love logic, so I figured I’d enjoy working through it.</p> <p>I created the buttons, elements, and was then distracted by some fun code that changed the color of the background with a click. Then after a good night’s sleep I was finally able to traverse through my saved clicks and calculate for two numbers and an operator. Yay!</p> <p>My next issue was crafting logic to handle double digit or longer numbers. I solved this by traversing my saved clicks and creating a stoplight that either concated the additional numbers into one index or didn’t.</p> <p>The next problem was multiple operators and thus multiple calculations. After my original result from the first three clicks (eg 8x8) I ended up then traversing by twos, starting one index after I ended (index 4) that way I could always look back and use the previous operator (index) with my current number.</p> <p>I considered writing logic alerting for multiple operators at once, and although I did alert if first click was with an operator, I left the rest to sort themselves out - just like any normal calculator you’d sat on.</p> <p>The brief asked for console logs of the numbers (and if you’d like to check the math, they’re still there) but I decided to update the welcome message innertext to the calculated total. It also includes a tada emoji for funsies.</p> <p>All in all it was a fun challenge that pushed me to go farther in my use of JS and CSS. </p> <p>If you'd like to use it here's the Replit:<br> <a href="proxy.php?url=https://replit.com/@Meg-Div/Calculator#calculator.js" rel="noopener noreferrer">https://replit.com/@Meg-Div/Calculator#calculator.js</a></p> <p>Here's the code: <a href="proxy.php?url=https://github.com/Meg-Div/DigitalCraftsProjects/tree/main/calculator" rel="noopener noreferrer">https://github.com/Meg-Div/DigitalCraftsProjects/tree/main/calculator</a></p> javascript beginners css showdev One day code challenge Meg Sat, 03 Dec 2022 17:51:42 +0000 https://dev.to/megdiv/one-day-code-challenge-bmj https://dev.to/megdiv/one-day-code-challenge-bmj <p>I decided to set myself a one day code challenge to see if I could re-create a game and chose Quiddler, which is one of my all time favorite word games. </p> <p>If you’d like to read about it, here’s the wiki page: <a href="proxy.php?url=https://en.wikipedia.org/wiki/Quiddler" rel="noopener noreferrer">https://en.wikipedia.org/wiki/Quiddler</a></p> <p>I decided I wanted a shorter game for my version, since there could be up to ten copies of the same card in the original card game and also got rid of the cards with two letters on them, such as “er” along any rules around additional players since my version would be single player.</p> <p>I settled on 10 rounds, with an automatic 50 point bonus if you used all the letters in your hand. You start with one vowel, one consonant, and one random card. The random cards are twice as likely to be a consonant over a vowel, but are entirely random and you receive a new one each turn.</p> <p>By far the largest issue I ran into was that Python didn’t seem to have an importable complete English dictionary. The closest I could find, Enchant, did not work on my version of VS Code but did work on Replit (after a 10 second updating delay).</p> <p>Is Enchant a complete dictionary? No, words like “fake” and “mutt” don’t seem to be recognized, but in testing it, enough words were accepted that I continued on. The final product is a short spelling game I hope you enjoy.</p> <p>Link to code: <a href="proxy.php?url=https://github.com/Meg-Div/DigitalCraftsProjects/tree/main/spelling-game" rel="noopener noreferrer">https://github.com/Meg-Div/DigitalCraftsProjects/tree/main/spelling-game</a></p> <p>Link to game: <a href="proxy.php?url=https://replit.com/@Meg-Div/quiddler#main.py" rel="noopener noreferrer">https://replit.com/@Meg-Div/quiddler#main.py</a></p> python beginners showdev