Lawrence  Lesch

Lawrence Lesch

1621674240

JavaScript Algorithm: Simple Pig Latin

Translate a string to Pig Latin

We are going to write a function called pigIt that will accept a string, str, as an argument.

You are given a string and the goal of the function is to translate the string to Pig Latin. To translate the string, you do the following:

  1. Move the first letter of the word to the end of the word.
  2. Add “ay” to the end of the word.

That’s it. If the word is only a single letter, skip step number one and just add an “ay” at the end. If the string is a punctuation mark or a number, leave it as is. Leave the cases of the words untouched.

Example:

pigIt('Pig latin is cool'); \\ igPay atinlay siay oolcay
pigIt('Hello world !'); \\ elloHay orldway !

To begin, we will split the string into an array where each word is its own array element. We assign that array to strArr.

let strArr = str.split(' ');

Next, we will create an empty array called pigLatin. This is the array we will append each word to after we translate it to Pig Latin.

let pigLatin = [];

#coding #programming #javascript #algorithms

What is GEEK

Buddha Community

JavaScript Algorithm: Simple Pig Latin

Javascript-algorithms: JavaScript Algorithms and Data Structures

JavaScript Algorithms and Data Structures


This repository contains JavaScript based examples of many popular algorithms and data structures.

Each algorithm and data structure has its own separate README with related explanations and links for further reading (including ones to YouTube videos).

Read this in other languages: 简体中文, 繁體中文, 한국어, 日本語, Polski, Français, Español, Português, Русский, Türkçe, Italiana, Bahasa Indonesia, Українська, Arabic, Tiếng Việt, Deutsch

☝ Note that this project is meant to be used for learning and researching purposes only, and it is not meant to be used for production.

Data Structures

A data structure is a particular way of organizing and storing data in a computer so that it can be accessed and modified efficiently. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.

B - Beginner, A - Advanced

Algorithms

An algorithm is an unambiguous specification of how to solve a class of problems. It is a set of rules that precisely define a sequence of operations.

B - Beginner, A - Advanced

Algorithms by Topic

Algorithms by Paradigm

An algorithmic paradigm is a generic method or approach which underlies the design of a class of algorithms. It is an abstraction higher than the notion of an algorithm, just as an algorithm is an abstraction higher than a computer program.

How to use this repository

Install all dependencies

npm install

Run ESLint

You may want to run it to check code quality.

npm run lint

Run all tests

npm test

Run tests by name

npm test -- 'LinkedList'

Troubleshooting

If linting or testing is failing, try to delete the node_modules folder and re-install npm packages:

rm -rf ./node_modules
npm i

Also make sure that you're using a correct Node version (>=14.16.0). If you're using nvm for Node version management you may run nvm use from the root folder of the project and the correct version will be picked up.

Playground

You may play with data-structures and algorithms in ./src/playground/playground.js file and write tests for it in ./src/playground/__test__/playground.test.js.

Then just simply run the following command to test if your playground code works as expected:

npm test -- 'playground'

Useful Information

References

Big O Notation

Big O notation is used to classify algorithms according to how their running time or space requirements grow as the input size grows. On the chart below you may find most common orders of growth of algorithms specified in Big O notation.

Big O graphs

Source: Big O Cheat Sheet.

Below is the list of some of the most used Big O notations and their performance comparisons against different sizes of the input data.

Big O NotationTypeComputations for 10 elementsComputations for 100 elementsComputations for 1000 elements
O(1)Constant111
O(log N)Logarithmic369
O(N)Linear101001000
O(N log N)n log(n)306009000
O(N^2)Quadratic100100001000000
O(2^N)Exponential10241.26e+291.07e+301
O(N!)Factorial36288009.3e+1574.02e+2567

Data Structure Operations Complexity

Data StructureAccessSearchInsertionDeletionComments
Array1nnn 
Stacknn11 
Queuenn11 
Linked Listnn1n 
Hash Table-nnnIn case of perfect hash function costs would be O(1)
Binary Search TreennnnIn case of balanced tree costs would be O(log(n))
B-Treelog(n)log(n)log(n)log(n) 
Red-Black Treelog(n)log(n)log(n)log(n) 
AVL Treelog(n)log(n)log(n)log(n) 
Bloom Filter-11-False positives are possible while searching

Array Sorting Algorithms Complexity

NameBestAverageWorstMemoryStableComments
Bubble sortnn2n21Yes 
Insertion sortnn2n21Yes 
Selection sortn2n2n21No 
Heap sortn log(n)n log(n)n log(n)1No 
Merge sortn log(n)n log(n)n log(n)nYes 
Quick sortn log(n)n log(n)n2log(n)NoQuicksort is usually done in-place with O(log(n)) stack space
Shell sortn log(n)depends on gap sequencen (log(n))21No 
Counting sortn + rn + rn + rn + rYesr - biggest number in array
Radix sortn * kn * kn * kn + kYesk - length of longest key

Project Backers

You may support this project via ❤️️ GitHub or ❤️️ Patreon.

Folks who are backing this project ∑ = 0

ℹ️ A few more projects and articles about JavaScript and algorithms on trekhleb.dev

Download Details:

Author: trekhleb
Source Code: https://github.com/trekhleb/javascript-algorithms 
License: MIT license

#javascript #computerscience #algorithm #algorithms 

Lawrence  Lesch

Lawrence Lesch

1621674240

JavaScript Algorithm: Simple Pig Latin

Translate a string to Pig Latin

We are going to write a function called pigIt that will accept a string, str, as an argument.

You are given a string and the goal of the function is to translate the string to Pig Latin. To translate the string, you do the following:

  1. Move the first letter of the word to the end of the word.
  2. Add “ay” to the end of the word.

That’s it. If the word is only a single letter, skip step number one and just add an “ay” at the end. If the string is a punctuation mark or a number, leave it as is. Leave the cases of the words untouched.

Example:

pigIt('Pig latin is cool'); \\ igPay atinlay siay oolcay
pigIt('Hello world !'); \\ elloHay orldway !

To begin, we will split the string into an array where each word is its own array element. We assign that array to strArr.

let strArr = str.split(' ');

Next, we will create an empty array called pigLatin. This is the array we will append each word to after we translate it to Pig Latin.

let pigLatin = [];

#coding #programming #javascript #algorithms

JavaScript Snake Game Tutorial - Develop a Simple Snake Game

Work on real-time JavaScript Snake game project and become a pro

Snake game is an interesting JavaScript project for beginners. Snake game is a single-player game, which we’ve been playing for a very long time. The game mainly consists of two components – snake and fruit. And we just need to take our snake to the food so that it can eat and grow faster and as the number of fruits eaten increases, the length of snake increases which makes the game more interesting. While moving if the snake eats its own body, then the snake dies and the game ends. Now let’s see how we can create this.

JavaScript Project Prerequisites

To implement the snake game in JavaScript you should have basic knowledge of:

1. Basic concepts of JavaScript

2. HTML

3. CSS

Download Project Code

Before proceeding ahead please download source code of Snake Game: Snake Game in JavaScript

Steps to Build the Project – JavaScript Snake game

1. Create Html file

HTML builds the basic structure. This file contains some basic HTML tags like div, h1, title, etc. also we’ve used bootstrap (CDN is already included).

index.html:

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DataFlair Snake game</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link rel="stylesheet" href="static/style.css">
</head>
<body>
    <div class="container">
        <div class ="Jumbotron">
        <h1>DataFlair Snake game using vanilla JavaScript</h1>

            <h2 class="btn btn-info"> 
                Score:   0 
            </h2>

            <div class="containerCanvas">
                <canvas id="canvas" width="500" height="500" class="canvasmain"> </canvas>
            </div>
        </div>
    </div>
    <script src="static/fruit.js"></script>
    <script src="static/snake.js"></script>
    <script src="static/draw.js"></script>
</body>
</html>

We have used simple HTML tags except tag. This new tag was introduced in HTML5, which is used to draw graphics, via JavaScript. It is nothing without JavaScript so we will use it in our JavaScript file to actually draw the board and the objects required. We’ll see its implementation in JS files.

#javascript tutorial #javascript project #javascript snake game #simple snake game #javascript

Santosh J

1622036598

JavaScript compound assignment operators

JavaScript is unarguablly one of the most common things you’ll learn when you start programming for the web. Here’s a small post on JavaScript compound assignment operators and how we use them.

The compound assignment operators consist of a binary operator and the simple assignment operator.

The binary operators, work with two operands. For example a+b where + is the operator and the a, b are operands. Simple assignment operator is used to assign values to a variable(s).

It’s quite common to modify values stored in variables. To make this process a little quicker, we use compound assignment operators.

They are:

  • +=
  • -+
  • *=
  • /=

You can also check my video tutorial compound assignment operators.

Let’s consider an example. Suppose price = 5 and we want to add ten more to it.

var price = 5;
price = price + 10;

We added ten to price. Look at the repetitive price variable. We could easily use a compound += to reduce this. We do this instead.

price += 5;

Awesome. Isn’t it? What’s the value of price now? Practice and comment below. If you don’t know how to practice check these lessons.

Lets bring down the price by 5 again and display it.
We use console.log command to display what is stored in the variable. It is very help for debugging.
Debugging let’s you find errors or bugs in your code. More on this later.

price -= 5;
console.log(price);

Lets multiply price and show it.

price *=5;
console.log(price);

and finally we will divide it.

price /=5;
console.log(price);

If you have any doubts, comment below.

#javascript #javascript compound assignment operators #javascript binary operators #javascript simple assignment operator #doers javascript

CSS Boss

CSS Boss

1606912089

How to create a calculator using javascript - Pure JS tutorials |Web Tutorials

In this video I will tell you How to create a calculator using javascript very easily.

#how to build a simple calculator in javascript #how to create simple calculator using javascript #javascript calculator tutorial #javascript birthday calculator #calculator using javascript and html