Web Development: Organizing Files and Folders

When you begin to build your website, it's a very clever idea to organize  your files and folders efficiently.

You should have:
  • A site root folder, inside of which you have:
    • An index.html file - that's the HTML for your homepage
    • A CSS folder
    • An images folder
    • A JavaScript folder. 
    • Other .html webpages for that website.

For Loops in JavaScript

Below is an example of  a for loop from JavaScript. Have a look and see if you understand how it works: 

var fruits = ["peach", "orange", "Apple"];

for (var i = 0; i<fruits.length; i++)
{
//we are now inside our loop.
document.write("value of i is: " + i);

//this is the html for a new line.
document.write("<br>");

document.write("the fruit at i is: " + fruits[i]);

document.write("<br>");
}

Note on DreamWeaver CC Libraries

When inserting images from the CC library in Adobe DreamWeaver, it's usually better to 'Download Linked' or Download Unlinked' images than to 'Insert Linked' or 'Insert Unlinked'. The reason; if you  insert a linked or unlinked image, it will be changed if anyone changes the image in Adobe's CC library.  However, if you download the image, the image is now inserted on your website independently.

JavaScript Logical Operators (and Boolean)

  • && (AND)
  • || (OR)
  • ! (NOT)
If you were to find out if two values were true or false, you would use AND. 

For something to be true, both the values would have to be true. 

For example, requesting if:

true && true = true
true && false = false
false && false = false 

|| (OR) is requesting if one of the values is true. For example: 

true || fsadf = true
false || true = true
false || false = false 

! (NOT) is a negation, so requesting if:
!true = false (something not true is false)
!false = true (something not false is true)

You can also combine logical operators.  For example:
 !(true && true) = false (because true AND true is true, and negating it is false) 

For more on operators, go to https://jamesymcjamesface.blogspot.com/2020/04/javascript-operators.html

JavaScript Operators

With JavaScript, a number is just a number (unlike in Python where you have integers, float and complex numbers).

The operators that can be used with numbers in JavaScript are:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (the remainders from division).

Note: if you say:
var x = 5
var y = 3
x + y  = 8

But if you say:
var "x" = 5
var "y" = 3
x + y = 53

In the latter, it returns 53, because you have defined x and y as strings by placing them in quotation marks.

There are other special variable operators: 
++ (This will increment the variable by 1): x = 5... x ++ = 6
-- (This will decrease the variable by 1): x = 5... x-- = 4

Comparisons:
== (equal to)
=== (equal value and type) this is unique to Javascript, and will return if the var is equal in number and type.  For example, if  you ask if "5" === 5, it will return 'false' as one is a string and the other is a number. However, if you ask if "5"==5, it will return 'true' despite the fact that they are different types.
!= (not equal to)
!== (not equal value or not equal type)
> (greater than)
>= (greater than or equal to)
< (less than)
<= (less than or equal to)

See about JavaScript logical operators at https://jamesymcjamesface.blogspot.com/2020/04/javascript-logical-operators-and-boolean.html

JavaScript - Reserved Words

Some words are reserved in JavaScript and cannot be used as variable names.  Some common examples are:

  • case
  • catch
  • for
  • let
  • private
  • this
  • with
  • in 
  • do
 Using these words as variables (var) will present you with an error for your code.

Reset Your CSS Styles for All Browsers

It is a very good idea when creating a new webpage to reset all styles for the browser.  This ensures that all browsers are starting off on the same footing when they read your CSS an HTML files, and ensures a level of consistency across all browsers.

Code for this can be found at mayerweb.com:

https://meyerweb.com/eric/tools/css/reset/
There, you can also find an excellent blog piece by Mayer, about why this is necessary. 

Content/Border Box Model (CSS)

Border Box Model
The padding occurs inside your content box (that is, inside the area your text or other content is contained) and it puts a 'pad' around it.

The margin occurs outside of your content box. This is an area that divides your content box (including any padding) from other content boxes.

A border occurs between the padding and the margin.

Border box is useful to use on its own instead of padding and margin, especially if you are doing responsive web design.

Using 'em' for sizing images in CSS

'em' is a relative measurement in CSS - relative, literally, to the px size of a letter 'm' in your text. This makes it a useful metric for sizing and resizing images for different situations, and still maintaining their proportion relative to the rest of your webpage.

There is also something called Root em - 'rem' which is useful for responsive websites.  It asks 'what was the original size of 'em' in order that it can correctly proportion the website.

However, a good rule-of-thumb is to use ordinary pixels (px) as a measurement for various elements on your webpage - at least starting off.

Aliases in SQL

Did you know you can list your tables as aliases in SQL?

Say you have a table called "quiz" with unique user_id's.

You could list your table as "q" (or whatever you like) and call its user_id column in the following way:
SELECT q.user_id
FROM quiz q
LIMIT 10;

Did you see that?  Where you place your table name ("quiz"), you can place your variable name after it, i.e. "q". 

This tidies up your code a little bit.  Say you had two other columns called "question" and "answer" - you could select the three columns by using the code: 
SELECT q.user_id, q.question, q.answer
FROM quiz q
LIMIT 10; 

Below is some example code from Codecademy's Funnels excercises on their Data Science course: 



SELECT q.user_id, 
h.user_id IS NOT NULL AS 'is_home_try_on', 
  h.number_of_pairs, 
  p.user_id IS NOT NULL AS 'is_purchase'
FROM quiz q
LEFT JOIN home_try_on h
ON q.user_id = h.user_id
LEFT JOIN purchase p
ON p.user_id = q.user_id
LIMIT 10; 

Here is a link to the W3 School's examples of how to create SQL aliases: https://www.w3schools.com/sql/sql_alias.asp

Percentiles with Numpy

First, a warning.  Don't get mixed-up between finding the percentage of x in a list and finding a percentile of x.  I've already covered the code of how to get percentages
find_percentages = np.mean(array_name < 81) 
which will return the percent of elements that are less than 81 in an array. 

Numpy has a function to find percentiles from arrays. 

It takes two arguments: the variable name for the array you are exploring, and the percentile you would like to retrieve from it.  The code looks like this: find_percentile = np.percentile(array_name, 40) - the fortieth percentile of this array will be the output. 

Quartiles, Inter-quartile and Median. 

Codecademy has given this elaboration of percentiles: 

Some percentiles have specific names:
  • The 25th percentile is called the first quartile
  • The 50th percentile is called the median
  • The 75th percentile is called the third quartile
The minimum, first quartile, median, third quartile, and maximum of a dataset are called a five-number summary. This set of numbers is a great thing to compute when we get a new dataset.
The difference between the first and third quartile is a value called the interquartile range. For example, say we have the following array:
d = [1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 8]
We can calculate the 25th and 75th percentiles using np.percentile:
np.percentile(d, 25) >>> 3.5 np.percentile(d, 75) >>> 6.5
Then to find the interquartile range, we subtract the value of the 25th percentile from the value of the 75th:
6.5 - 3.5 = 3
50% of the dataset will lie within the interquartile range. The interquartile range gives us an idea of how spread out our data is. The smaller the interquartile range value, the less variance in our dataset. The greater the value, the larger the variance.

NumPy: Averages of Data Sets

What are the meanings of the various types of averages in datasets?

  • Mean == the "centre" ("center") of a dataset. 
    • If you have an array: array_1 = np.array([1,2,3,4,5]), the mean of this array would be 3, because 1 + 2 +3 + 4 + 5 = 15, 15 / 5(numbers in array) = 3. Thus, 3 would be the average or the mean of this particular list.
    • The mean is affected by outliers
  • Median == the "middle" of a dataset. 
    • If you have a list [1, 1, 4, 7, 8, 9,  9], then 7 would be the median of the list, as it is literally halfway between the minimum value and the maximum value. 
    • If you have a list whose length is an even number, say [1, 2,  2,  3, 4,  5, 5, 7] (8 numbers), then the median is the half-way point between the two middle numbers (in this case 3 & 4), so the median of the list above would be 3.5. 
    • Of course, we're likely to be dealing with very large lists and arrays, so working out the middle numbers ourselves would become a very tedious task.  We can overcome this by using the np.median function. 
    • The median is not affected by outliers

Finding Percentages: 

You can use numpy in conjunction with the mean function to work out percentages from a given dataset: np.mean. You can do so using logical operators.  For example, if you have an np.array_example = [15, 18, 9, 5, 4, 21, 10, 16] and you wanted to find out the percentage of elements greater than 10, you could do so by using: 
>>>np.mean(array_example > 10)
0.5
 
The above result is 0.5 or 50%.

Why does this work?  Well, the code is using a logical operator to iterate through the array data.  Where an element is greater than 10 it is equal to 1 (or True).  Where it is equal to, or not greater than 10, it is equal to 0 (or False). The mean function then takes the number of results equal to 1 and divides them by the number of elements in the list (in this case the answer would be 4 / 8 = 0.5). In other words, 50% of elements in the array are equal to True, which in this case is the same as saying 50% of elements in the array are greater than 10. 





Web Development: Organizing Files and Folders

When you begin to build your website, it's a very clever idea to organize  your files and folders efficiently. You should have: A ...