JavaScript | Coding Challenge Set #1 For Beginners

Hello JavaScript code newbie! In this article I’m proposing you a series of JavaScript coding challenge that will help you practice the basic language constructs and algorithms. You can also read our JavaScript | Intro To Job Titles

Note: This blog is for Beginners

Coding Challenge Set #1 For Beginners

These JavaScript challenge are specially designed for beginners, therefore the proposed solutions are implemented using only simple and classical JavaScript programming elements.

If you are not bounded with time limit, we recommend you to solve the challenges on by yourself first before viewing solutions and then compare your solution to the provided solution. If you don’t have enough time to code yourself, just click on “View Code” and analyze the solutions!

List of JavaScript coding challenge 1 to 26

26: Create a function that will receive two arrays and will return an array with elements that are in the first array but not in the second using JavaScript.

Thank you for reading and stay tune, stay connected and stay safe. Please support us by sharing the blog.

Useful blogs JavaScript | Intro To Job Titles

Read our other blogs on ITbulls.in

Mishan Goti

Mean Stack Developer
"I am a developer with 2+ years of experience in developing web applications and solutions for many domestic and international clients."

Previous Post
Next Post

for(var i = 1; i <= 10; i++)
{
console.log(i);
}

for(var i = 1; i <= 100; i += 2)
{
console.log(i);
}

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

for(var i = 1; i <= 10; i++)
{
printTable(i);
console.log("");
}

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

var sum = 0;

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

console.log(sum);

var prod = 1;

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

console.log(prod);

var sum = 0;

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

console.log(sum);

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

var r = celsiusToFahrenheit(20);
console.log(r);

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

var r = fahrenheitToCelsius(68);
console.log(r);

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);
console.log(sum);

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);

console.log("Average: ", avg);

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);
console.log(ar2);

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);
console.log(ar2);

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

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

function findMax(ar)
{
var max = ar;
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);
console.log("Max: ", max);

var f0 = 0;
console.log(f0);

var f1 = 1;
console.log(f1);

for(var i = 2; i < 10; i++)
{
var fi = f1 + f0;
console.log(fi);

f0 = f1;
f1 = fi;
}

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

if (n == 1)
return 1;

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

var n = findFibonacci(10);
console.log(n);

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;
}

console.log(2, " is prime? ", isPrime(2));
console.log(3, " is prime? ", isPrime(3));
console.log(4, " is prime? ", isPrime(4));
console.log(5, " is prime? ", isPrime(5));
console.log(9, " is prime? ", isPrime(9));

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);
console.log("Sum: ", sum);

printPrimes(100);

// Function prints the first nPrimes numbers
function printPrimes(nPrimes)
{
var n = 0;
var i = 2;

while(n < nPrimes)
{
if (isPrime(i))
{
console.log(n, " --> ", i);
n++;
}

i++;
}
}

// Returns true if a number is prime
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;
}

console.log(getPrimes(10, 100));

function getPrimes(nPrimes, startAt)
{
var ar = [];

var i = startAt;

while(ar.length < nPrimes)
{
if (isPrime(i))
{
ar.push(i);
}

i++;
}

return ar;
}

// Returns true if a number is prime
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;
}

var ar = [1, 2, 3];
rotateLeft(ar);
console.log(ar);

function rotateLeft(ar)
{
var first = ar.shift();
ar.push(first);
}

var ar = [1, 2, 3];
rotateRight(ar);
console.log(ar);

function rotateRight(ar)
{
var last = ar.pop();
ar.unshift(last);
}

var ar = [1, 2, 3];
var ar2 = reverseArray(ar);
console.log(ar2);

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

for(var i = ar.length - 1; i >= 0; i--)
{
ar2.push(ar[i]);
}

return ar2;
}

var s = reverseString("JavaScript");
console.log(s);

function reverseString(s)
{
var s2 = "";

for(var i = s.length - 1; i >= 0; i--)
{
var char = s[i];
s2 += char;
}

return s2;
}

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);
console.log("Sum: ", sum);

var ar1 = [1, 2, 3, 10, 5, 3, 14];
var ar2 = [1, 4, 5, 6, 14];

var ar = mergeExclusive(ar1, ar2);
console.log(ar);

function mergeExclusive(ar1, ar2)
{
var ar = [];

for(let el of ar1)
{
if (!ar2.includes(el))
{
ar.push(el);
}
}

for(let el of ar2)
{
if (!ar1.includes(el))
{
ar.push(el);
}
}

return ar;
}

var ar1 = [1, 2, 3, 10, 5, 3, 14];
var ar2 = [-1, 4, 5, 6, 14];

var ar = mergeLeft(ar1, ar2);
console.log(ar);

function mergeLeft(ar1, ar2)
{
var ar = [];

for(let el of ar1)
{
if (!ar2.includes(el))
{
ar.push(el);
}
}

return ar;
}