How Do You Reverse a String?

Dec 17, 2023

Steps:

  • Create a empty string newString
  • Create a for loop, the starting point of the loop with be str.length - 1 which will be the last character of the string
  • Continue for loop till i is greater than or equals 0, decrement i after each iteration
function reverseString(str) {
    let newString = "";

   for(let i = str.length - 1 ; i >= 0; i--) {
       newString = newString + str[i];
   }

    return newString;
}

reverseString('hello');

References:

Categories : JavaScript   Interview   Coding

Check if string is anagram?

Dec 17, 2023

Steps:

  • Pass two strings word and anagram
  • Iterate over first string word, get character at i
  • If character present in anagram, then remove character from anagram
  • 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:

Categories : JavaScript   Interview   Coding

Margin Collapsing

Jul 13, 2021

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:

Categories : CSS

Difference between ECMAScript and JavaScript

Jul 12, 2021

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:

Categories : JavaScript

CSS Box Model

Jul 6, 2021

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:

Categories : CSS   Web