Explore 1.5M+ audiobooks & ebooks free for days

Only $12.99 CAD/month after trial. Cancel anytime.

200+ JavaScript Programs for Beginners
200+ JavaScript Programs for Beginners
200+ JavaScript Programs for Beginners
Ebook296 pages1 hour

200+ JavaScript Programs for Beginners

Rating: 0 out of 5 stars

()

Read preview

About this ebook

"JavaScript is the Most popular programming language used by professional developers today"


?Programming in JS was complicated until you appreciated this Wonderful book.

Focus on the most important thing writing code, starting from the first program to the last will help you quickly Increase your skills as a programmer and will give you the knowledge necessary to understand:
✅The flow of a program
✅the syntax
✅the direction
✅what keywords Add to your code
✅make great decisions
and much more.
Once you have solved a certain number of programs, you will be able to develop yourself and turn your own ideas into a reality without even having to think about it.

Buy NOW and Transform your Coding Skills!

LanguageEnglish
PublisherHernando Abella
Release dateJan 12, 2025
ISBN9798227516756
200+ JavaScript Programs for Beginners
Author

Hernando Abella

Hernando Abella is a developer who thoroughly enjoys sharing all the knowledge he has accumulated through his extensive experience. After completing his studies at INCCA University of Colombia, he has dedicated himself to writing programming languages, including Java, C, C++,C#, among others. He has been immersed in the world of programming since the age of 14 and has always harbored a profound passion for coding. his hobbies include cycling and swimming. More About me on : X : Hernando Abella

Read more from Hernando Abella

Related to 200+ JavaScript Programs for Beginners

Related ebooks

Programming For You

View More

Reviews for 200+ JavaScript Programs for Beginners

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    200+ JavaScript Programs for Beginners - Hernando Abella

    Intro

    Programming in JS was complicated until you appreciated this wonderful book.

    Focus on the most important thing writing code, starting from the first program to the last will help you quickly increase your skills as a programmer and will give you the knowledge necessary to understand:

    The flow of a program, the syntax, the direction, how it works, what Add keywords to your code, make great decisions, and much more.

    This is a very essential guide with which you can learn to create more than... Once you have solved a certain number of programs, you will be able to develop yourself and turn your own ideas into a reality without even having to think about it.

    1. Print Hello World

    The Hello World program is a classic in programming and is often the first program written when learning a new programming language. In this case, the goal is to print the text string Hello, World! to the browser console.

    console.log(Hello, World!);

    2. Add Two Numbers

    This program focuses on taking two numbers as input and then displaying their sum. It prompts the user to enter two numbers, adds them together, and prints the result.

    let num1 = parseFloat(prompt(Enter the first number:));

    let num2 = parseFloat(prompt(Enter the second number:));

    if (!isNaN(num1) && !isNaN(num2)) {

    let sum = num1 + num2;

    console.log(`The sum of ${num1} and ${num2} is: ${sum}`);

    } else {

    console.log(Please enter valid numbers.);

    }

    3. Find the Square Root

    This program prompts the user to enter a number and calculates its square root. It then displays the result, ensuring that the entered number is non-negative.

    let inputNumber = parseFloat(prompt(Enter a non-negative number:));

    if (!isNaN(inputNumber) && inputNumber >= 0) {

    let squareRoot = Math.sqrt(inputNumber);

    console.log(`The square root of ${inputNumber} is: ${squareRoot}`);

    } else {

    console.log(Please enter a valid non-negative number.);

    }

    4. Calculate the Area of a Triangle

    This program prompts the user to enter the base and height of a triangle and calculates its area using the formula: Area=12×Base×HeightArea=21​×Base×Height. It then displays the calculated area.

    let base = parseFloat(prompt(Enter the base of the triangle:));

    let height = parseFloat(prompt(Enter the height of the triangle:));

    if (!isNaN(base) && !isNaN(height) && base > 0 && height > 0) {

    let area = 0.5 * base * height;

    console.log(

    `The area of the triangle with base ${base} and height ${height} is: ${area}`

    );

    } else {

    console.log(Please enter valid positive numbers for base and height.);

    }

    5. Swap Two Variables

    This program prompts the user to enter two variables and swaps their values. It then displays the variables before and after the swap.

    let variable1 = prompt(Enter the first variable:);

    let variable2 = prompt(Enter the second variable:);

    console.log(

    `Before swapping: Variable1 = ${variable1}, Variable2 = ${variable2}`

    );

    // Swapping the variables

    let temp = variable1;

    variable1 = variable2;

    variable2 = temp;

    console.log(

    `After swapping: Variable1 = ${variable1}, Variable2 = ${variable2}`

    );

    6. Solve Quadratic Equation

    This program solves a quadratic equation of the form ax^2 + bx + c = 0, where a, b, and c are coefficients. It uses the quadratic formula to find the roots of the equation.

    // Prompt user for coefficients

    let a = parseFloat(prompt(Enter the coefficient a:));

    let b = parseFloat(prompt(Enter the coefficient b:));

    let c = parseFloat(prompt(Enter the coefficient c:));

    // Calculate the discriminant

    let discriminant = b ** 2 - 4 * a * c;

    // Check if roots are real

    if (!isNaN(a) && !isNaN(b) && !isNaN(c)) {

    if (discriminant > 0) {

    let root1 = (-b + Math.sqrt(discriminant)) / (2 * a);

    let root2 = (-b - Math.sqrt(discriminant)) / (2 * a);

    console.log(

    `The roots of the quadratic equation are: ${root1} and ${root2}`

    );

    } else if (discriminant === 0) {

    let root = -b / (2 * a);

    console.log(`The quadratic equation has a repeated root: ${root}`);

    } else {

    console.log(The quadratic equation has complex roots.);

    }

    } else {

    console.log(Please enter valid numbers for coefficients.);

    }

    7. Convert Kilometers to Miles

    This program prompts the user to enter a distance in kilometers and converts it to miles using the conversion factor: 1 kilometer is approximately equal to 0.621371 miles. It then displays the converted distance.

    // Prompt user for distance in kilometers

    let kilometers = parseFloat(prompt(Enter the distance in kilometers:));

    // Conversion factor

    const kilometersToMilesConversionFactor = 0.621371;

    // Check if input is a valid number

    if (!isNaN(kilometers)) {

    // Convert kilometers to miles

    let miles = kilometers * kilometersToMilesConversionFactor;

    console.log(`${kilometers} kilometers is approximately ${miles} miles.`);

    } else {

    console.log(Please enter a valid number for the distance in kilometers.);

    }

    8. Convert Celsius to Fahrenheit

    This program converts a temperature from Celsius to Fahrenheit using the formula: F = (C * 9/5) + 32, where F is the temperature in Fahrenheit and C is the temperature in Celsius.

    // Prompt user for temperature in Celsius

    let celsius = parseFloat(prompt(Enter the temperature in Celsius:));

    // Check if input is a valid number

    if (!isNaN(celsius)) {

    // Convert Celsius to Fahrenheit

    let fahrenheit = (celsius * 9) / 5 + 32;

    console.log(

    `${celsius} degrees Celsius is equal to ${fahrenheit} degrees Fahrenheit.`

    );

    } else {

    console.log(Please enter a valid number for the temperature in Celsius.);

    }

    9. Generate a Random Number

    This program generates a random number between a user-defined range. It prompts the user to enter the minimum and maximum values of the range, and then it displays the generated random number within that range.

    // Prompt user for the range

    let minRange = parseFloat(prompt(Enter the minimum value of the range:));

    let maxRange = parseFloat(prompt(Enter the maximum value of the range:));

    // Check if input is a valid number

    if (!isNaN(minRange) && !isNaN(maxRange) && minRange < maxRange) {

    // Generate a random number within the specified range

    let randomNumber = Math.random() * (maxRange - minRange) + minRange;

    console.log(

    `A random number between ${minRange} and ${maxRange} is: ${randomNumber}`

    );

    } else {

    console.log(

    Please enter valid numbers, ensuring that the minimum value is less than the maximum value.

    );

    }

    10. Check if a number is Positive, Negative, or Zero

    This program prompts the user to enter a number and checks whether it is positive, negative, or zero. It then displays the result.

    // Prompt user for a number

    let number = parseFloat(prompt(Enter a number:));

    // Check if input is a valid number

    if (!isNaN(number)) {

    // Check if the number is positive, negative, or zero

    if (number > 0) {

    console.log(`${number} is a positive number.`);

    } else if (number < 0) {

    console.log(`${number} is a negative number.`);

    } else {

    console.log(The entered number is zero.);

    }

    } else {

    console.log(Please enter a valid number.);

    }

    11. Check if a Number is Odd or Even

    This program prompts the user to enter a number and checks whether it is odd or even. It then displays the result.

    // Prompt user for a number

    let number = parseInt(prompt(Enter a number:));

    // Check if input is a valid integer

    if (!isNaN(number) && Number.isInteger(number)) {

    // Check if the number is odd or even

    if (number % 2 === 0) {

    console.log(`${number} is an even number.`);

    } else {

    console.log(`${number} is an odd number.`);

    }

    } else {

    console.log(Please enter a valid integer.);

    }

    12. Find the Largest Among Three Numbers

    This program prompts the user to enter three numbers and determines and displays the largest among them.

    // Prompt user for three numbers

    let num1 = parseFloat(prompt(Enter the first number:));

    let num2 = parseFloat(prompt(Enter the second number:));

    let num3 = parseFloat(prompt(Enter the third number:));

    // Check if inputs are valid numbers

    if (!isNaN(num1) && !isNaN(num2) && !isNaN(num3)) {

    // Find the largest among the three numbers

    let largestNumber = Math.max(num1, num2, num3);

    console.log(

    `The largest number among ${num1}, ${num2}, and ${num3} is: ${largestNumber}`

    );

    } else {

    console.log(Please enter valid numbers for all three inputs.);

    }

    13. Check Prime Number

    This program prompts the user to enter a number and checks whether it is a prime number or not. It then displays the

    Enjoying the preview?
    Page 1 of 1