<?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: Kaleb</title>
    <description>The latest articles on DEV Community by Kaleb (@mbkaleb).</description>
    <link>https://dev.to/mbkaleb</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%2F818906%2F4e10dde4-fdb7-44b4-997a-d23072744524.jpg</url>
      <title>DEV Community: Kaleb</title>
      <link>https://dev.to/mbkaleb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mbkaleb"/>
    <language>en</language>
    <item>
      <title>Stephen Wolfram, Wolfram|Alpha + ChatGPT</title>
      <dc:creator>Kaleb</dc:creator>
      <pubDate>Thu, 09 Feb 2023 18:43:17 +0000</pubDate>
      <link>https://dev.to/mbkaleb/stephen-wolfram-wolframalpha-chatgpt-4i3e</link>
      <guid>https://dev.to/mbkaleb/stephen-wolfram-wolframalpha-chatgpt-4i3e</guid>
      <description>&lt;h2&gt;
  
  
  Why Should I care?
&lt;/h2&gt;

&lt;p&gt;If you want to skip to the meat, jump to 'Today', you might want to read the 'Wolfram|Alpha' section as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Young Wolfram
&lt;/h2&gt;

&lt;p&gt;At the age of 15 he began research in applied quantum field theory and particle physics. He went on to get published in 4 separate, professional scientific journals. By the age of 18 he wrote 10 academic, pier reviewed papers relating to his field of study. To say the least he was a prodigy.&lt;/p&gt;

&lt;h2&gt;
  
  
  CEO of Wolfram Research
&lt;/h2&gt;

&lt;p&gt;Steven Wolfram is a physicist turned software genius. At the age of 24 he dropped the academia of particle physics for the school of Natural Sciences. His new field of study was cellular automata with computer simulations. Once he left academia in 1987 he founded Wolfram Research, Inc. They launched Wolfram Alpha in 2009.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wolfram|Alpha
&lt;/h2&gt;

&lt;p&gt;To keep this short Wolfram|Alpha is a computation model + database. It's goal is to generate logical computed answers from built-in data, algorithms and methods. The difference between this and chatGPT is there is no input to the data from users. The output of W|A is intended to be dry, factual data. Its purpose is to replace search engines and become a more effective way to access un-opinionated information.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Interesting fact: Wolfram|Alpha is used by Siri as part of its knowledge base.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Today
&lt;/h2&gt;

&lt;h4&gt;
  
  
  ChatGPT and Wolfram|Alpha
&lt;/h4&gt;

&lt;p&gt;Stephen W. compares cGPT to "human like" computation wherein its understanding of logic comes purely from language. He posits this as a flaw. The logic accuracy of ChatGPT is limited not only by its textual training data, reinforcement training, but also by its generative token and probabilistic choices. Wolfram compares this to a human without scientific and mathematical skills. &lt;/p&gt;

&lt;p&gt;In his paper on the subject we see a few examples of how chatGPT fails on basic logic-based questions. It responds in a convincing essay style response, while the answers are completely wrong. One question to cGPT was: "What is 3 to the power of 73?" &lt;/p&gt;

&lt;p&gt;ChatGPT: "3 to the power of 73 is approximately 14,091,714,236..." + some essay about the response&lt;/p&gt;

&lt;p&gt;The correct integer is ~67.58 decillion, chatGPT was completely wrong. &lt;br&gt;
&lt;em&gt;*Wolfram responds with the correct exact integer&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It's here that the concept is clear, combine the language model of ChatGPT with the super-computation of Wolfram|Alpha. The crisp, precise nature of W|A logic, responses and the combination of cGPTs language based generative text might be the next step toward AGI.&lt;/p&gt;

&lt;p&gt;If you have some patience I would recommend you read the paper from Wolfram himself. He has a slight ego you have to read around, however.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Some projects have already integrated some generative text (chatGPT) and answer engines (Wolfram|Alpha) &lt;/p&gt;

&lt;p&gt;&lt;a href="https://huggingface.co/spaces/JavaFXpert/Chat-GPT-LangChain" rel="noopener noreferrer"&gt;Hugging Face&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Though the generative response is a bit &lt;em&gt;light&lt;/em&gt;, and slow.  I would say it's more a display of the limit of chatGPT or the LangChain API used to combine the AI models.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Stephen_Wolfram" rel="noopener noreferrer"&gt;Wikipedia/Stephen_Wolfram&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.wolframalpha.com/about" rel="noopener noreferrer"&gt;About Wolfram|Alpha&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://writings.stephenwolfram.com/2023/01/wolframalpha-as-the-way-to-bring-computational-knowledge-superpowers-to-chatgpt/" rel="noopener noreferrer"&gt;Steven Wolfram Publications&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.siriuserguide.com/what-is-wolfram-alpha/" rel="noopener noreferrer"&gt;Siri and Wolfram|Alpha&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://writings.stephenwolfram.com/2023/01/wolframalpha-as-the-way-to-bring-computational-knowledge-superpowers-to-chatgpt/" rel="noopener noreferrer"&gt;Stephen Wolfram: "Wolfram|Alpha as the Way to Bring Computational Knowledge Superpowers to ChatGPT"&lt;/a&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
    <item>
      <title>α/β Pruning</title>
      <dc:creator>Kaleb</dc:creator>
      <pubDate>Wed, 27 Jul 2022 19:40:17 +0000</pubDate>
      <link>https://dev.to/mbkaleb/ab-pruning-54h3</link>
      <guid>https://dev.to/mbkaleb/ab-pruning-54h3</guid>
      <description>&lt;p&gt;&lt;em&gt;This post is meant to prepare me for my next project, a chess AI.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Weights
&lt;/h2&gt;

&lt;p&gt;Humans are very different from computers. What we lack in raw processing power, we make up for intuition. Computers are the very opposite. With no intuition or subconscious, everything is either explicit or calculated. Modern AI uses weights from explicit rules to train a neural net. This is some imitation of intuition. I will attempt to use this method to quickly determine if a move is good or bad.&lt;br&gt;
See &lt;a href="https://www.deepmind.com/publications/weight-uncertainty-in-neural-networks"&gt;Weight In Uncertainty&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Challenge
&lt;/h2&gt;

&lt;p&gt;The challenge of a modern chess AI is complex in many different aspects. The framework you begin with defines how effective the program may perform, not just from a time-space-complexity standpoint, but also from a competitive perspective.&lt;/p&gt;

&lt;h3&gt;
  
  
  The premise:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Teach an AI:
&lt;/h4&gt;

&lt;p&gt;How the pieces move,&lt;br&gt;
  How to take other pieces,&lt;br&gt;
  Where the pieces are the most valuable,&lt;br&gt;
  How to open,  &lt;em&gt;more on this later&lt;/em&gt;&lt;br&gt;
  How to checkmate,&lt;br&gt;
  How to determine if a move is good or bad.&lt;/p&gt;

&lt;h3&gt;
  
  
  Q:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;What data structures are required? &lt;/li&gt;
&lt;li&gt;How do we reduce big(O)?&lt;/li&gt;
&lt;li&gt;How do we quickly evaluate a move? &lt;/li&gt;
&lt;li&gt;How do we find the best move set?&lt;/li&gt;
&lt;li&gt;What makes a good move?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This final objective is clearly the most difficult to accomplish and the reason for this blog. First, what makes a move "good"? &lt;/p&gt;

&lt;p&gt;The entire premise of chess is to move your pieces in a way that gives you a checkmate against your opponent. The best way to think of this is through position. The "position" is the way the pieces are arranged. If you have the better position you will win. &lt;/p&gt;

&lt;h3&gt;
  
  
  3,5: So if a move improves our position, it is good.
&lt;/h3&gt;

&lt;p&gt;To evaluate a position we must create rules and weights based on the first five objectives: How the pieces move, how to capture pieces, where they are the most valuable, how to open like a pro, and how checkmate is possible.&lt;/p&gt;

&lt;h4&gt;
  
  
  A position is evaluated on these five weights.
&lt;/h4&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;Now we just have to evaluate every possible position with these weights, ~10^100... That's a one with 100 zeros.&lt;br&gt;
How about the first six moves? After some math, it's about 9,132,484 distinct positions. What gives we can even see a few moves ahead without some insane number of positions. Most games of chess last at least 20 moves, some even in the hundreds. How can we reduce the number of positions to evaluate without a major loss of competitive potential? &lt;/p&gt;

&lt;h2&gt;
  
  
  1,2,4 The Solution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  α/β Pruning
&lt;/h3&gt;

&lt;p&gt;This gives us the answer to questions 1 and 2. What is alpha-beta pruning? α/β Pruning is a search algorithm that reduces the number of nodes on a tree based on some explicit or evaluated rules. Say you generate a layer of nodes on a tree with values 1-10. The pruning then removes any node with a value of less than 3. This removes 1/3 of nodes and your next layer is 1/3 * children smaller. If half of all chess moves are objectively bad the problem becomes more linear instead of some runaway value of 10^100 moves. &lt;/p&gt;

&lt;p&gt;Thanks for reading and have a wonderful day.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Most Hated PGL</title>
      <dc:creator>Kaleb</dc:creator>
      <pubDate>Thu, 05 May 2022 23:11:00 +0000</pubDate>
      <link>https://dev.to/mbkaleb/the-most-hated-pgl-aki</link>
      <guid>https://dev.to/mbkaleb/the-most-hated-pgl-aki</guid>
      <description>&lt;h2&gt;
  
  
  VBA and Friends
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;75% of users &lt;em&gt;hate&lt;/em&gt; Visual Basic A (classic)&lt;/li&gt;
&lt;li&gt;Its intended &lt;em&gt;(and only legitimate)&lt;/em&gt; purpose exists inside of a product&lt;/li&gt;
&lt;li&gt;Microsoft is awful (duh)&lt;/li&gt;
&lt;li&gt;Sad attempt at object oriented development&lt;/li&gt;
&lt;li&gt;Insanely slow&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Its performance is terrible and no serious projects should be written in this language.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Extremely difficult to learn as a new programmer
&lt;/h3&gt;

&lt;h2&gt;
  
  
  # "VBA has a place in development because it is easier to learn."
&lt;/h2&gt;

&lt;p&gt;The opposite seems to be true:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unique logic and syntax&lt;/li&gt;
&lt;li&gt;Poor OOC support&lt;/li&gt;
&lt;li&gt;Lots of macros, meaning that lots of code is hiding under the hood and difficult to debug.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  But why would you HATE a programing language?
&lt;/h3&gt;

&lt;p&gt;To put it simply, "...it is entirely written by idiots" -Stuart McCormack on &lt;em&gt;Visual Basic Application&lt;/em&gt;. The slow nature of its macros and irritating lack-of quality of life features cements most users from apathetic to hateful.&lt;/p&gt;

&lt;p&gt;Sources:&lt;br&gt;
&lt;a href="https://datascopic.net/5-against-vba/"&gt;https://datascopic.net/5-against-vba/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.quora.com/Is-VBA-a-bad-language"&gt;https://www.quora.com/Is-VBA-a-bad-language&lt;/a&gt;&lt;br&gt;
&lt;a href="https://boxbase.org/entries/2014/jul/28/vba/"&gt;https://boxbase.org/entries/2014/jul/28/vba/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;apologists: &lt;br&gt;
&lt;a href="https://rubberduckvba.wordpress.com/2019/04/10/whats-wrong-with-vba/"&gt;https://rubberduckvba.wordpress.com/2019/04/10/whats-wrong-with-vba/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>webdev</category>
      <category>vba</category>
      <category>programming</category>
    </item>
    <item>
      <title>Rails Quick-Ref VOL. 1</title>
      <dc:creator>Kaleb</dc:creator>
      <pubDate>Tue, 05 Apr 2022 06:05:18 +0000</pubDate>
      <link>https://dev.to/mbkaleb/rails-quick-start-on-ubuntu-140e</link>
      <guid>https://dev.to/mbkaleb/rails-quick-start-on-ubuntu-140e</guid>
      <description>&lt;h2&gt;
  
  
  What is rails?
&lt;/h2&gt;

&lt;p&gt;Ruby on rails is technically a full-stack framework built on top of ruby. Meaning that it has the tools to build both front and backend apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use rails?
&lt;/h2&gt;

&lt;p&gt;Rails is best suited for quickly creating an API with custom logic within a MVC development structure. The generators allow you to quickly setup all files from the command line and connects them via &lt;a href="https://guides.rubyonrails.org/active_record_basics.html"&gt;Active Record&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting into it:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;add rails to your gem bundle, with &lt;code&gt;gem install rails&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;rails new my-app&lt;/code&gt; or bootstrap from chosen directory&lt;/li&gt;
&lt;li&gt;run your rails server with rails server&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;build your app&lt;/em&gt;
From here you can everything from a full stack app to a simple API and everything in-between.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Helpful Commands
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rails s&lt;/code&gt;&lt;br&gt;
&lt;em&gt;start the server of current directory&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rails db:seed&lt;/code&gt;&lt;br&gt;
&lt;em&gt;populate the database from seed file&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rails db:migrate&lt;/code&gt;&lt;br&gt;
&lt;em&gt;update the schema file from the migration files&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rails new project-name options&lt;/code&gt;&lt;br&gt;
ex: &lt;code&gt;rails new Hacker-API&lt;/code&gt;&lt;br&gt;
&lt;em&gt;create a new rails directory&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rails d &amp;lt;name of generator&amp;gt; &amp;lt;class_name&amp;gt;&lt;/code&gt;&lt;br&gt;
ex: &lt;code&gt;rails d resource Shop&lt;/code&gt;&lt;br&gt;
&lt;em&gt;delete a specified generator&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Rails g&lt;/code&gt; -lists the generators&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rails g &amp;lt;generator&amp;gt; &amp;lt;model name&amp;gt; &amp;lt;attribute&amp;gt; &amp;lt;options&amp;gt; &amp;lt;flag&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Generators
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;migration: creates a migration&lt;/li&gt;
&lt;li&gt;model: creates a migration and model&lt;/li&gt;
&lt;li&gt;controller: creates a migration, model, and controller&lt;/li&gt;
&lt;li&gt;resource: creates migration, model, controller, and routes
&lt;em&gt;These generators can be found in your config folder&lt;/em&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  attribute
&lt;/h3&gt;

&lt;p&gt;Here we define an attribute of our model:&lt;br&gt;
&lt;code&gt;attrname:string&lt;/code&gt;&lt;br&gt;
&lt;code&gt;name:DATATYPE&lt;/code&gt;&lt;br&gt;
with the attribute name and its data type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Options
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--no-test-framework&lt;/code&gt; &lt;em&gt;exclude the test file&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;helpful links: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.solutelabs.com/blog/ruby-on-rails-7"&gt;What's new with Ruby on Rails 7&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/10605967/creating-an-initializer"&gt;sauce&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/vinistock/understanding-and-writing-rails-generators-10h1"&gt;more sauce&lt;/a&gt;&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Ruby_on_Rails"&gt;Ruby on rails Wikepedia&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Escaping X InnerHTML</title>
      <dc:creator>Kaleb</dc:creator>
      <pubDate>Sun, 06 Mar 2022 04:00:20 +0000</pubDate>
      <link>https://dev.to/mbkaleb/escaping-x-innerhtml-3fbl</link>
      <guid>https://dev.to/mbkaleb/escaping-x-innerhtml-3fbl</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;any comments HIGHLY appreciated; feedback/questions welcome!&lt;/em&gt;
&lt;/h4&gt;




&lt;h2&gt;
  
  
  Escape and Data Channels
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;What is escape code? Why do we use it?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Escape code attempts to solve a complex yet foundational issue on how a computer reads and handles data as a stream, like printing or executing. When we see a " &lt;em&gt;quote&lt;/em&gt;, just one, it's just some text, but to a computer, it is an entire command, a declaration of data! When we want to pass data, it's required to specify what type and how we need the computer to read it.&lt;/p&gt;

&lt;p&gt;Escape code example:&lt;br&gt;
&lt;code&gt;'&amp;amp;#60;imgsrc&amp;amp;#61;&amp;amp;#34;x&amp;amp;#34;onerror&amp;amp;#61;&amp;amp;#34;alert&amp;amp;#40;1&amp;amp;#41;&amp;amp;#34;&amp;amp;#62;';&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When providing a user an input field, the computer has to take the input and store it somehow, in what we know as a variable. Then when we go back and tell the computer to read the variable; we call said variable, and we have our assignment/call loop. When we assign an input DIRECTLY to a variable and call it we are &lt;em&gt;executing&lt;/em&gt; code.&lt;/p&gt;

&lt;p&gt;In a smart component, we cannot avoid the manipulation of data through these streams and therefore MUST use some form of &lt;em&gt;escape code&lt;/em&gt;. Our escape code can help us interpret strings and inputs properly, not just allowing us to write code freely, but also protect users against &lt;em&gt;xSS&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  innerHTML and &lt;em&gt;Injection&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;wait, what?&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First, we need to know how innerHTML works and then we move to why we can inject JS to this element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Secondly, Injection is defined as running JavaScript from the client-side invoked by the client&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, when our JS source for a site reads innerHTML data, it doesn't just send some data for an HTML file to execute. The innerHTML executes the value defined by the expression, this means if you use &lt;/p&gt;

&lt;p&gt;&lt;code&gt;element.innerHTML = &amp;lt;input /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When we put &lt;code&gt;&amp;lt;script&amp;gt; &amp;lt;/script&amp;gt;&lt;/code&gt; tags inside our input, we can VERY easily inject code, directly to the site, like so:&lt;br&gt;
&lt;code&gt;const HACKS=&lt;/code&gt; &lt;code&gt;&amp;lt;script deferred&amp;gt; alert("xScriptatK");&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;em&gt;You need to defer because as the page loads the DOM has already executed&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is how most hackers steal site data: cookies, localStorage, sometimes make fetch() requests for altering data, and then return data to a remote location.&lt;/p&gt;







&lt;h2&gt;
  
  
  &lt;strong&gt;ESCAPING X INNERHTML&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;All Together Now:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we want to dynamically set web-data and content we need to use a markup of  &lt;em&gt;escaped&lt;/em&gt; characters. &lt;/p&gt;

&lt;p&gt;We have a few options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;textContent&lt;/code&gt;&lt;br&gt;
Renders a string of escaped characters, our HACKS var from earlier would render as a string and not execute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sanitize 3rd Party Content&lt;br&gt;
Let's say we have an input field, and the user fills it with the following, &lt;code&gt;'&amp;lt;img src="x" onerror=HACKS()&amp;gt;'&lt;/code&gt; This would send an error and allow them to inject code from the error call, in this case, its &lt;code&gt;HACKS()&lt;/code&gt; Instead, we call a sanitizer on the content with an escape method it will become:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;`app.innerHTML =&lt;br&gt;
'&amp;lt;imgsrc="x"error="alert(1)"&amp;gt;';&lt;/p&gt;

&lt;p&gt;This is the encoded syntax of &lt;br&gt;
&lt;code&gt;&amp;lt;img src="x" onerror="HACKS"&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/x" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/x"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(our hacks)&lt;/p&gt;

&lt;p&gt;and we remove any markup and get our source back without any injection.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var sanitizeHTML = function (str) {&lt;br&gt;
    return str.replace(/[^\w. ]/gi, function (c) {&lt;br&gt;
        return '&amp;amp;#' + c.charCodeAt(0) + ';';&lt;br&gt;
    });&lt;br&gt;
};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This code takes user-submitted (str) and encodes it (sanitizes it) and returns another string(str) the cleaned-up version.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const Hac = '" onerror="alert(\'XSS Attack\')"';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//Inject our dynamic element into the UI&lt;br&gt;
app.innerHTML = '&amp;lt;img src="' + sanitizeHTML(thirdPartySrc) + '"&amp;gt;';&lt;/code&gt;&lt;br&gt;
This is just an example of encoding a property, if you want to allow some markup you will need a library of whitelisted elements/methods.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use a framework/library
Using a library like REACT, Angular, or jQuery.
Libraries tend use #2 under the hood, and use understandable syntax to make your code more understandable, and writeable. This is the best option in my opinion, as they are VERY well tested and should be considered reliable by any coder worth his/her/they/whomst/nonbinary markup.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Roll Credits&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Now next time you are using a library/framework you can appreciate all the work that is done for you by escape code, handled by a library. &lt;br&gt;
HUGE THANKS TO:&lt;/em&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML" rel="noopener noreferrer"&gt;Element.innerHTML&lt;/a&gt;&lt;br&gt;
&lt;a href="https://gomakethings.com/how-to-sanitize-third-party-content-with-vanilla-js-to-prevent-cross-site-scripting-xss-attacks/" rel="noopener noreferrer"&gt;gomakethings&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.utilities-online.info/urlencode" rel="noopener noreferrer"&gt;Decoder&lt;/a&gt;&lt;br&gt;
Without these resources I would have no idea where to start with this, and they were highly informative in this learning process.&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>datascience</category>
    </item>
    <item>
      <title>Ternary X LEETcode</title>
      <dc:creator>Kaleb</dc:creator>
      <pubDate>Mon, 21 Feb 2022 16:15:23 +0000</pubDate>
      <link>https://dev.to/mbkaleb/js-ternary-operator-x-leet-code-2180</link>
      <guid>https://dev.to/mbkaleb/js-ternary-operator-x-leet-code-2180</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Ternary What?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The ternary operator is a unique tool that give us a way to execute code based upon a conditional in our operation. The ternary operator is unique in the matter of its operands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The condition &lt;/li&gt;
&lt;li&gt;expression executed if condition is truthy&lt;/li&gt;
&lt;li&gt;expression executed if condition is falsy
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;condition ? exprIfTrue : exprIfFalse
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives us a lot of options with a such simple operator, when we realize that we can nest the operators inside of an expression, things get more... complicated.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Right-associative logic&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The right-associative logic is a result of grouping and short-circuit execution. The logic is executed in a way that all conditions must met before ANY logic is evaluated inside the expression branch, therefore:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;a ? b : c ? d : e&lt;/code&gt; &lt;em&gt;is evaluated as&lt;/em&gt; &lt;code&gt;a ? b : (c ? d : e)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;consider:&lt;/em&gt; &lt;code&gt;z = ((a == b ? a : b) ? c : d);&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;If we try to use left-associate logic, this example would be impossible to execute with some complex made up math we don't care about, instead: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;first condition:&lt;/em&gt; &lt;code&gt;if (a==b) z=a&lt;/code&gt;&lt;br&gt;
&lt;em&gt;second condition:&lt;/em&gt; &lt;code&gt;if(b) z = c;&lt;/code&gt;&lt;br&gt;
&lt;em&gt;final condition:&lt;/em&gt; &lt;code&gt;z=d&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we took the left execution as read, our code would wire like:&lt;/p&gt;

&lt;p&gt;`int z, tmp;&lt;/p&gt;

&lt;p&gt;if(a == b) tmp = a;&lt;br&gt;
else       tmp = b;&lt;/p&gt;

&lt;p&gt;if(tmp) z = c;&lt;br&gt;
else    z = d;&lt;br&gt;
`&lt;br&gt;
??????&lt;br&gt;
Maybe I'm not too sure, but this doesn't work because we need a made up variable to fill in our logic, so we can consider that &lt;em&gt;imaginary&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The idea is that instead of nesting stacks of what amounts to if-else &lt;em&gt;stacks&lt;/em&gt;, we simply add conditionals to our operator to execute from our original variable, in this case z.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Ternary Operator Vs. If Else&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There is no major difference between runtime big O memory allocation between a ternary and a well written if/else(as I have seen evidence for). There is some debate to which style is faster, or more readable. Not up for debate is the fact that the Ternary operator is more professional outside of loops and will bring your code to a higher standard.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Why not just use an if/else to make things much easier? 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Well, we could nest huge amounts of these conditionals, at a cost. The readability of our code is somewhat subjective, and utilizing the ternary operator is one of the tools in our arsenal to achieve that impossible goal of readable JS. &lt;/p&gt;

&lt;p&gt;credit to &lt;a href="https://stackoverflow.com/users/60777/chris-lutz"&gt;Chris Lutz: &lt;/a&gt;&lt;a href="https://stackoverflow.com/questions/7407273/why-is-the-conditional-operator-right-associative"&gt; "Why is the conditional operator right associative?"&lt;/a&gt;&lt;/p&gt;

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