Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Mastering Frontend Interview Coding: JavaScript and CSS Essentials

Tech 1

Centering Elements in CSS

Method 1: Absolute Positioning with Calculated Margins

When dimensions are known:

.parent {
  width: 500px;
  height: 500px;
  position: relative;
}
.child {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 150px;
  left: 150px;
}

Method 2: Aboslute Positioning with Auto Margin

For unknown dimensions:

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
}

Method 3: Transform-Based Centering

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Method 4: Flexbox Alignment

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

Method 5: Table-Cell Vertical Alignment

.parent {
  display: table-cell;
  vertical-align: middle;
}
.child {
  margin: 0 auto;
}

Method 6: Inlline-Block with Line Height

.parent {
  text-align: center;
  line-height: 500px;
}
.child {
  display: inline-block;
}

Two-Column Layout Techniques

Three-Column Layout Patterns

Double Wing Layout

<header>Header</header>
<div class="main">
  <div class="center">Main Content</div>
</div>
<div class="left">Left Sidebar</div>
<div class="right">Right Sidebar</div>
<footer>Footer</footer>
.main {
  width: 100%;
  float: left;
  background-color: skyblue;
}
.center {
  margin: 0 100px;
}
.left {
  width: 100px;
  background-color: green;
  float: left;
  margin-left: -100%;
}
.right {
  width: 100px;
  background-color: red;
  float: left;
  margin-left: -100px;
}

Holy Grail Layout

<header>Header</header>
<div class="wrapper clearfix">
  <div class="center">Main Content</div>
  <div class="left">Left Sidebar</div>
  <div class="right">Right Sidebar</div>
</div>
<footer>Footer</footer>
.clearfix::after {
  content: '';
  display: block;
  clear: both;
}
.wrapper {
  padding: 0 100px;
}
.center {
  width: 100%;
  background-color: skyblue;
  float: left;
}
.left {
  background-color: green;
  float: left;
  width: 100px;
  margin-left: -100%;
  position: relative;
  left: -100px;
}
.right {
  width: 100px;
  background-color: red;
  float: left;
  margin-right: -100px;
}

Triptych Layout

CSS Shape Creation

Triangle

.triangle {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid #f00;
}

Circle

.circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: #000;
}

Checkerboard Pattern

.checker {
  width: 200px;
  height: 200px;
  background-image: 
    linear-gradient(45deg, #ccc 25%, transparent 25%),
    linear-gradient(-45deg, #ccc 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #ccc 75%),
    linear-gradient(-45deg, transparent 75%, #ccc 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 0, 10px 10px, 10px 10px;
}

JavaScript Core Function Implementations

Custom Promise Constructor

function CustomPromise(executor) {
  const instance = this;
  instance.status = 'pending';
  instance.result = null;
  instance.callbacks = [];

  function resolve(value) {
    if (instance.status !== 'pending') return;
    instance.status = 'fulfilled';
    instance.result = value;
    setTimeout(() => {
      instance.callbacks.forEach(cb => cb.onFulfilled(value));
    }, 0);
  }

  function reject(reason) {
    if (instance.status !== 'pending') return;
    instance.status = 'rejected';
    instance.result = reason;
    setTimeout(() => {
      instance.callbacks.forEach(cb => cb.onRejected(reason));
    }, 0);
  }

  try {
    executor(resolve, reject);
  } catch (error) {
    reject(error);
  }
}

Promise.all Implementation

CustomPromise.all = function(promises) {
  return new CustomPromise((resolve, reject) => {
    const results = [];
    let completedCount = 0;

    promises.forEach((promise, index) => {
      CustomPromise.resolve(promise).then(
        value => {
          results[index] = value;
          completedCount++;
          if (completedCount === promises.length) resolve(results);
        },
        reason => reject(reason)
      );
    });
  });
};

Promise.race Implementation

CustomPromise.race = function(promises) {
  return new CustomPromise((resolve, reject) => {
    promises.forEach(promise => {
      CustomPromise.resolve(promise).then(resolve, reject);
    });
  });
};

Promise.resolve

CustomPromise.resolve = function(value) {
  return new CustomPromise((resolve, reject) => {
    if (value instanceof CustomPromise) {
      value.then(resolve, reject);
    } else {
      resolve(value);
    }
  });
};

Promise.reject

CustomPromise.reject = function(reason) {
  return new CustomPromise((resolve, reject) => {
    reject(reason);
  });
};

Function.call Emulation

Function.prototype.myCall = function(context, ...args) {
  const ctx = context || globalThis;
  const uniqueKey = Symbol();
  ctx[uniqueKey] = this;
  const result = args.length > 0 ? ctx[uniqueKey](...args) : ctx[uniqueKey]();
  delete ctx[uniqueKey];
  return result;
};

Function.apply Emulation

Function.prototype.myApply = function(context, args = []) {
  const ctx = context || globalThis;
  const uniqueKey = Symbol();
  ctx[uniqueKey] = this;
  const result = args.length > 0 ? ctx[uniqueKey](...args) : ctx[uniqueKey]();
  delete ctx[uniqueKey];
  return result;
};

Function.bind Emulation

Function.prototype.myBind = function(context, ...boundArgs) {
  const self = this;
  return function(...additionalArgs) {
    return self.myCall(context, ...boundArgs, ...additionalArgs);
  };
};

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.