Arrow Functions in JavaScript / ES6

What are Arrow Functions in JavaScript / ECMAScript 6?

Arrow functions are a simplified short hand method for creating anonymous functions.

Older ES5 JavaScript example:

function setup() {
    createCanvas(600, 400);
    background(0);
    let button = createButton('press')
    button.mousePressed(changeBackground);

    function changeBackground() {
        background(random(255));
    }
}

New ES6 JavaScript Example

function setup() {
    createCanvas(600, 400);
    background(0);
    let button = createButton('press')
    button.mousePressed(() => background(255)));
}

Anonymous Function written as Arrow Function:

() => background(255))

Video: ES6 Arrow Function

How to Prevent Page Reload with Javascript onclick Without Using “#”

Just a very short blog jot for today as I’m busy working on a Salesforce.com data analysis tool / system.

Since this is a very low budget project, I’m hand coding everything using Notepad++ and VBScript to create a dynamic web application in old school Acitve Server Pages (ASP) that can consume the analytic data that my system stored on a MS Access database.

Again, low budget. Me and my wits pounding out code.

I ran in to an issue where the web page is calling a JavaScript that passes two variables to an Ajax call to load a Chart.js bar chart in a popup window. 

It was working great but the page that had the onlick event to load the popup chart was reloading as well when clicked instead of just loading the popup window with the chart.

What’s the fix?

Add “return false;” at the like in the example below and the page containing the onclick link should stop reloading when clicked.

Old Code

<a href="#" OnClick="getChart('John Smith', '2017-01');">John Smith</a>

New Code

<a href="" OnClick="getChart('John Smith', '2017-01');return false;">John Smith</a>

I always give credit to the people who helped me find the answer.  I had found the answer in the stackoverflow page below.

https://stackoverflow.com/questions/17680436/how-to-prevent-reload-with-onclick-without

For Loops for Beginners – ASP, C#, PHP, JavaScript & Python Examples

A “For Loop” executes a block of code a specific number of times or while a specified condition is true.

PHP For Loop

for (init; condition; increment)
  {
  code to be executed;
  }

php fOR lOOP PARAMETERS

  • init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
  • condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment: Mostly used to increment a counter (but can be any code to be executed at the end of the iteration)

Note: The init and increment parameters above can be empty or have multiple expressions (separated by commas). Example The example below defines a loop that starts with i=1. The loop will continue to run as long as the variable i is less than, or equal to 5. The variable i will increase by 1 each time the loop runs:

PHP For Loop Example Code

<?php
for ($i=1; $i<=5; $i++)
  {
  echo("The number is " . $i . "<br>");
  }
?>

Classic ASP For Loop Example Code

<%
For i = 1 to 5
 Response.Write("The number is " & i & "<br>")
Next
%>

JavaScript For Loop Example

<%
For i = 1 to 5
 Response.Write("The number is " & i & "<br>")
Next
%>

C# For Loop Example

for (int i = 0; i < 5; i++) 
      {
        Console.WriteLine(i);
      }    

Python For Loop Example

states = ["Alaska", "Alabama", "Arkansas"]
for x in states:
  print(x) 
  if x == "Alabama":
    break

3 Ways to Reverse a String using JavaScript

3 JavaScript Code Examples for Reversing a String

function reverseString1(str) {    
  //This way uses an array to reverse the string
  var strArr = str.split("");
  var reverseStringArray = strArr.reverse();
  var reversedString = reverseStringArray.join("");
  return reversedString;
}

reverseString1("hello")

//*******************************************
function reverseString2(str) {
  //Single Line does it all
  return str.split("").reverse().join();
}

reverseString2("hello")


//*******************************************
function reverseString3(str) {    
  var final = "";
  for (var i = str.length - 1; i >= 0; i--) {
   final += str[i]
  }
  return final;
}

reverseString3("hello")