JavaScript Code Examples This repository contains a collection of JavaScript code snippets that demonstrate various programming concepts and techniques. Below is a summary of the examples included:
-
Basic Output Print "Hello World": Demonstrates three ways to print "Hello World" using console.log, alert, and document.write.
-
Basic Arithmetic Adding Two Numbers: Simple addition of two numbers and displaying the result.
User Input Addition: Takes two numbers from the user and calculates their sum.
- Mathematical Operations Square Root: Calculates the square root of a number provided by the user.
Area of a Triangle: Calculates the area of a triangle using base and height, and also using Heron's formula for any type of triangle.
- Swapping Values Swapping with Temporary Variable: Swaps the values of two variables using a temporary variable.
Swapping without Temporary Variable: Swaps the values of two variables without using a temporary variable.
- Unit Conversion Kilometers to Miles: Converts kilometers to miles (implementation available in HTML file).
Celsius to Fahrenheit: Converts Celsius to Fahrenheit (implementation available in HTML file).
- Number Checking Positive, Negative, or Zero: Checks if a number is positive, negative, or zero using Math.sign() and if-else statements.
Even or Odd: Determines if a number is even or odd using if-else and ternary operator.
-
Prime Number Check Prime Number: Checks if a number is prime or not.
-
Largest Number Largest of Three Numbers: Finds the largest number among three numbers using Math.max() and a custom function.
-
Factorial Calculation Factorial: Calculates the factorial of a number using a loop and recursion.
-
Multiplication Table Multiplication Table: Prints the multiplication table of a number.
-
Armstrong Number Armstrong Number: Checks if a number is an Armstrong number.
Armstrong Numbers in an Interval: Finds all Armstrong numbers within a given interval.
-
Palindrome Check Palindrome: Checks if a string is a palindrome using array methods and for loops.
-
Character Occurrence Character Occurrence: Counts the number of occurrences of a specific character in a string.
-
String Operations Starts and Ends With: Checks if a string starts and ends with specific characters.
Substring Check: Checks if a string contains a specific substring.
Capitalize First Letter: Converts the first letter of a string to uppercase.
-
Vowel Count Count Vowels: Counts the number of vowels in a string using regular expressions.
-
Sum of Natural Numbers Sum Using Recursion: Calculates the sum of natural numbers using recursion.
-
Factors of a Number Factors: Finds all factors of a given number.
-
Simple Calculator Calculator: A simple calculator using switch-case to perform basic arithmetic operations.
-
Fibonacci Sequence Fibonacci Sequence: Generates the Fibonacci sequence up to a specified number of terms.
-
String Manipulation Replace Characters: Replaces specific characters in a string using replace() and regular expressions.
Reverse String: Reverses a string using a loop.
- Sorting Sort Words Alphabetically: Sorts words in a string in alphabetical order.