Check if string is anagram?
Steps:
- Pass two strings
word
andanagram
- Iterate over first string
word
, get character ati
- If character present in
anagram
, then remove character fromanagram
- Iterate until
anagram
is empty
function isAnagram(word, anagram) {
if (word.length !== anagram.length) {
return false;
}
for (let i = 0; i < word.length; i++) {
let c = word[i];
let index = anagram.indexOf(c);
if (index === -1) {
return false;
}
anagram = anagram.substring(0, index) + anagram.substring(index + 1, anagram.length);
}
return anagram.length === 0;
}
isAnagram('hello', 'olleh');
References:
Margin Collapsing
The top and bottom margins of blocks are sometimes collapsed into a single margin whose size is the largest of the individual margins (or just one of them, if they are equal), a behavior known as margin collapsing.
References:
Difference between ECMAScript and JavaScript
ECMAScript is a language specification and JavaScript is the implementation of that specification.
ECMAScript is standardised by Ecma International.
The Ecma TC39 committee is responsible for evolving the ECMAScript programming language and authoring the specification. Changes to the language are developed by way of process. There are five stages, a strawperson stage, and 4 “maturity” stages. The TC39 committee must approve accetance for each stage.
References:
CSS Box Model
The CSS box model defines how the different parts of a box - margin, border, padding, and content - work together to create a box that can be seen on the page.
Parts of a box
- Content box - The area where content is displayed, which can be sized using properties like width and height.
- Padding box - The padding sits around the content as white space, its size is controlled using padding and related properties.
- Border box - The border box wraps the content and any padding. Its sizing and style can be controlled using border and related properties.
- Margin box - The margin is the outermost layer, wrapping the content, padding and border as whitespace between this box and other elements. Its size can be controlled using margin and related properties.
Standard CSS box model
In the standard box model, if a box is given width and height attribute, this defines the width and height of the content box.
Alternative CSS box model
Using this model, any width is the width of the visible box on the page, therefore the content area width is that the width minux the width for the padding and border.
References:
HTTP Cache
HTTP Cache improves performance of web sites and applications by reusing previously fetchted resources.
Cache-Control header
The server can return a Cache-Control
directive to specify how, and for
how long, the browser and other intermediate caches should cache the
individual response.
-
No caching
Cache-Control: no-store
This instructs the browser and other intermediate caches (like CDNs) to never store any version of the file. -
Cache but revalidate
Cache-Control: no-cache
This instructs the browser that it must revalidate with the server every time before using a cached version of the URL. -
Private and public caches
Cache-Control: private
Browser can cache the file but intermediate caches cannot.Cache-Control: public
The response can be stored by any cache. -
Expiration
Cache-Control: max-age=31536000
The maximum amount of time in which a resource will be considered fresh. This directive is relative to the time of request, and overrides the Expires header (if set). -
Validation
Cache-Control: must-revalidate
The cache must verify the status of stale resources before using it and expired ones should not be used.
ETags
The ETag (or Entity Tag) works in a similar way to the Last-Modified
header except its value is a digest of the resources contents (for instance,
an MD5 Hash). This allows the server to identify if the cached contents
of the resource are different to the most recent version.
If the Etag header was part of the response for a resource, the client
can issue an If-None-Match
in the header of future requests - in order
to validate the cached resource.
Last-Modified
This header servers the same purpose as ETag
, but uses a time-bsaed
strategy to determine if a resource has changed, as opposed to the
content-based strategy of ETag
. If the Last-Modified
header is
present in a response, then the client can issue an If-Modified-Since
request header to validate the cached document.