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.
https://codeguppy.com/code.html?mrgCtLGA90Ozr0Otrs5Z
for(var i = 1; i <= 10; i++)
{
println(i);
}
https://codeguppy.com/code.html?eDLA5XPp3bPxP79H2jKT
for(var i = 1; i <= 100; i += 2)
{
println(i);
}
https://codeguppy.com/code.html?fpnQzIhnGUUmCUZy1fyQ
for(var i = 1; i <= 10; i++)
{
var row = "7 * " + i + " = " + 7 * i;
println(row);
}
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);
}
}
https://codeguppy.com/code.html?Vy6u9kki2hXM4YjsbpuN
var sum = 0;
for(var i = 1; i <= 10; i++)
{
sum += i;
}
println(sum);
https://codeguppy.com/code.html?IIuJX4gnXOndNu0VrywA
var prod = 1;
for(var i = 1; i <= 10; i++)
{
prod *= i;
}
println(prod);
https://codeguppy.com/code.html?DcOffOyoIArmNZHVNM2u
var sum = 0;
for(var i = 11; i <= 30; i += 2)
{
sum += i;
}
println(sum);
https://codeguppy.com/code.html?oI5mWm6QIMRjY1m9XAmI
function celsiusToFahrenheit(n)
{
return n * 1.8 + 32;
}
var r = celsiusToFahrenheit(20);
println(r);
https://codeguppy.com/code.html?mhnf8DpPRqqgsBgbJNpz
function fahrenheitToCelsius(n)
{
return (n - 32) / 1.8;
}
var r = fahrenheitToCelsius(68);
println(r);
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);
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);
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);
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);
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);
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);
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;
}
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);
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));
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