Hello JavaScript code newbie! In this article I’m proposing you a series of coding challenges that will help you practice the basic language constructs and algorithms.

These coding challenges are intended for beginners, therefore the proposed solutions are implemented using only simple and classical programming elements. Each solution is accompanied by an online link that helps you quickly run it in a code playground at codeguppy.com

If you have time, we recommend you to solve the challenges on your own and then compare your solution to the provided solution. If time is limited, just browse through the solutions!

Note: The code is making use of the codeguppy specific function println() to print the results. If you want to run these solutions outside codeguppy.com, just replace println() with console.log() then run them using your browser console tool or node.js.

Coding challenge #1: Print numbers from 1 to 10

https://codeguppy.com/code.html?mrgCtLGA90Ozr0Otrs5Z

for(var i = 1; i <= 10; i++)
{
    println(i);
}

Coding challenge #2: Print the odd numbers less than 100

https://codeguppy.com/code.html?eDLA5XPp3bPxP79H2jKT

for(var i = 1; i <= 100; i += 2)
{
    println(i);
}

Coding challenge #3: Print the multiplication table with 7

https://codeguppy.com/code.html?fpnQzIhnGUUmCUZy1fyQ

for(var i = 1; i <= 10; i++)
{
    var row = "7 * " + i + " = " + 7 * i;
    println(row);
}

Coding challenge #4: Print all the multiplication tables with numbers from 1 to 10

https://codeguppy.com/code.html?78aD4mWSCzoNEVxOQ8tI

for(var i = 1; i <= 10; i++)
{
    printTable(i);
    println("");
}

function printTable(n)
{
    for(var i = 1; i <= 10; i++)
    {
        var row = n + " * " + i + " = " + n * i;
        println(row);
    }
}

Coding challenge #5: Calculate the sum of numbers from 1 to 10

https://codeguppy.com/code.html?Vy6u9kki2hXM4YjsbpuN

var sum = 0;

for(var i = 1; i <= 10; i++)
{
    sum += i;
}

println(sum);

Coding challenge #6: Calculate 10!

https://codeguppy.com/code.html?IIuJX4gnXOndNu0VrywA

var prod = 1;

for(var i = 1; i <= 10; i++)
{
    prod *= i;
}

println(prod);

Coding challenge #7: Calculate the sum of odd numbers greater than 10 and less than 30

https://codeguppy.com/code.html?DcOffOyoIArmNZHVNM2u

var sum = 0;

for(var i = 11; i <= 30; i += 2)
{
    sum += i;
}

println(sum);

Coding challenge #8: Create a function that will convert from Celsius to Fahrenheit

https://codeguppy.com/code.html?oI5mWm6QIMRjY1m9XAmI

function celsiusToFahrenheit(n)
{
    return n * 1.8 + 32;
}

var r = celsiusToFahrenheit(20);
println(r);

Coding challenge #9: Create a function that will convert from Fahrenheit to Celsius

https://codeguppy.com/code.html?mhnf8DpPRqqgsBgbJNpz

function fahrenheitToCelsius(n)
{
    return (n - 32) / 1.8;
}

var r = fahrenheitToCelsius(68);
println(r);

Coding challenge #10: Calculate the sum of numbers in an array of numbers

https://codeguppy.com/code.html?TteeVr0aj33ZyCLR685L

function sumArray(ar)
{
    var sum = 0;

    for(var i = 0; i < ar.length; i++)
    {
        sum += ar[i];
    }

    return sum;
}

var ar = [2, 3, -1, 5, 7, 9, 10, 15, 95];
var sum = sumArray(ar);
println(sum);

Coding challenge #11: Calculate the average of the numbers in an array of numbers

https://codeguppy.com/code.html?7i9sje6FuJsI44cuncLh

function averageArray(ar)
{
    var n = ar.length;
    var sum = 0;

    for(var i = 0; i < n; i++)
    {
        sum += ar[i];
    }

    return sum / n;
}

var ar = [1, 3, 9, 15, 90];
var avg = averageArray(ar);

println("Average: ", avg);

Coding challenge #12: Create a function that receives an array of numbers and returns an array containing only the positive numbers

Solution 1:

https://codeguppy.com/code.html?0eztj1v6g7iQLzst3Id3

function getPositives(ar)
{
    var ar2 = [];

    for(var i = 0; i < ar.length; i++)
    {
        var el = ar[i];

        if (el >= 0)
        {
            ar2.push(el);
        }
    }

    return ar2;
}

var ar = [-5, 10, -3, 12, -9, 5, 90, 0, 1];
var ar2 = getPositives(ar);

println(ar2);

Coding challenge #12: Create a function that receives an array of numbers and returns an array containing only the positive numbers

Solution 2

https://codeguppy.com/code.html?KefrPtrvJeMpQyrB8V2D

function getPositives(ar)
{
    var ar2 = [];

    for(var el of ar)
    {
        if (el >= 0)
        {
            ar2.push(el);
        }
    }

    return ar2;
}

var ar = [-5, 10, -3, 12, -9, 5, 90, 0, 1];
var ar2 = getPositives(ar);

println(ar2);

Coding challenge #12: Create a function that receives an array of numbers and returns an array containing only the positive numbers

Solution 3

https://codeguppy.com/code.html?qJBQubNA7z10n6pjYmB8

function getPositives(ar)
{
    return ar.filter(el => el >= 0);
}

var ar = [-5, 10, -3, 12, -9, 5, 90, 0, 1];
var ar2 = getPositives(ar);
println(ar2);

Coding challenge #13: Find the maximum number in an array of numbers

https://codeguppy.com/code.html?THmQGgOMRUj6PSvEV8HD

function findMax(ar)
{
    var max = ar[0];

    for(var i = 0; i < ar.length; i++)
    {
        if (ar[i] > max)
        {
            max = ar[i];
        }
    }

    return max;
}

var ar = [-5, 10, -3, 12, -9, 5, 90, 0, 1];
var max = findMax(ar);
println("Max: ", max);

Coding challenge #14: Print the first 10 Fibonacci numbers without recursion

https://codeguppy.com/code.html?rKOfPxHbVwxNWI2d8orH

var f0 = 0;
println(f0);

var f1 = 1;
println(f1);

for(var i = 2; i < 10; i++)
{
    var fi = f1 + f0;
    println(fi);

    f0 = f1;
    f1 = fi;
}

Coding challenge #15: Create a function that will find the nth Fibonacci number using recursion

https://codeguppy.com/code.html?IneuIg9O0rRV8V76omBk

function findFibonacci(n)
{
    if (n == 0)
        return 0;

    if (n == 1)
        return 1;

    return findFibonacci(n - 1) + findFibonacci(n - 2);
}

var n = findFibonacci(10);
println(n);

Coding challenge #16: Create a function that will return a Boolean specifying if a number is prime

https://codeguppy.com/code.html?fRYsPEc2vcZTbIU8MKku

function isPrime(n)
{
    if (n < 2)
        return false;

    if (n == 2)
        return true;

    var maxDiv = Math.sqrt(n);

    for(var i = 2; i <= maxDiv; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }

    return true;
}

println(2, " is prime? ", isPrime(2));
println(3, " is prime? ", isPrime(3));
println(4, " is prime? ", isPrime(4));
println(5, " is prime? ", isPrime(5));
println(9, " is prime? ", isPrime(9));

Coding challenge #17: Calculate the sum of digits of a positive integer number

https://codeguppy.com/code.html?RHA714FYio8gWgmjWYPz

function sumDigits(n)
{
    var s = n.toString();
    var sum = 0;

    for(var char of s)
    {
        var digit = parseInt(char);
        sum += digit;
    }

    return sum;
}

var sum = sumDigits(1235231);
println("Sum: ", sum);

#javascript #developer

JavaScript Practical Coding Challenges For Beginners
320.35 GEEK