visit
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 , just replace println() with console.log() then run them using your browser console tool or node.js.
for(var i = 1; i <= 10; i++)
{
println(i);
}
for(var i = 1; i <= 100; i += 2)
{
println(i);
}
for(var i = 1; i <= 10; i++)
{
var row = "7 * " + i + " = " + 7 * i;
println(row);
}
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);
}
}
var sum = 0;
for(var i = 1; i <= 10; i++)
{
sum += i;
}
println(sum);
var prod = 1;
for(var i = 1; i <= 10; i++)
{
prod *= i;
}
println(prod);
var sum = 0;
for(var i = 11; i <= 30; i += 2)
{
sum += i;
}
println(sum);
function celsiusToFahrenheit(n)
{
return n * 1.8 + 32;
}
var r = celsiusToFahrenheit(20);
println(r);
function fahrenheitToCelsius(n)
{
return (n - 32) / 1.8;
}
var r = fahrenheitToCelsius(68);
println(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);
println(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);
println("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);
println(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);
println(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);
println(ar2);
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);
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;
}
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);
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));
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);
printPrimes(100);
// Function prints the first nPrimes numbers
function printPrimes(nPrimes)
{
var n = 0;
var i = 2;
while(n < nPrimes)
{
if (isPrime(i))
{
println(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;
}
println(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);
println(ar);
function rotateLeft(ar)
{
var first = ar.shift();
ar.push(first);
}
var ar = [1, 2, 3];
rotateRight(ar);
println(ar);
function rotateRight(ar)
{
var last = ar.pop();
ar.unshift(last);
}
var ar = [1, 2, 3];
var ar2 = reverseArray(ar);
println(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");
println(s);
function reverseString(s)
{
var s2 = "";
for(var i = s.length - 1; i >= 0; i--)
{
var char = s[i];
s2 += char;
}
return s2;
}
var ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];
var ar = mergeArrays(ar1, ar2);
println(ar);
function mergeArrays(ar1, ar2)
{
var ar = [];
for(let el of ar1)
{
ar.push(el);
}
for(let el of ar2)
{
ar.push(el);
}
return ar;
}
var ar1 = [1, 2, 3, 10, 5, 3, 14];
var ar2 = [1, 4, 5, 6, 14];
var ar = mergeExclusive(ar1, ar2);
println(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);
println(ar);
function mergeLeft(ar1, ar2)
{
var ar = [];
for(let el of ar1)
{
if (!ar2.includes(el))
{
ar.push(el);
}
}
return ar;
}
var ar = getDistinctElements([1, 2, 3, 6, -1, 2, 9, 7, 10, -1, 100]);
println(ar);
function getDistinctElements(ar)
{
var ar2 = [];
for(let i = 0; i < ar.length; i++)
{
if (!isInArray(ar[i], ar2))
{
ar2.push(ar[i]);
}
}
return ar2;
}
function isInArray(n, ar)
{
for(let i = 0; i < ar.length; i++)
{
if (ar[i] === n)
return true;
}
return false;
}
var ar = getDistinctElements([1, 2, 3, 6, -1, 2, 9, 7, 10, -1, 100]);
println(ar);
function getDistinctElements(ar)
{
var ar2 = [];
var lastIndex = ar.length - 1;
for(let i = 0; i <= lastIndex; i++)
{
if (!isInArray(ar[i], ar, i + 1, lastIndex))
{
ar2.push(ar[i]);
}
}
return ar2;
}
function isInArray(n, ar, fromIndex, toIndex)
{
for(var i = fromIndex; i <= toIndex; i++)
{
if (ar[i] === n)
return true;
}
return false;
}
var n = 10;
println("Sum of first ", n, " primes is ", sumPrimes(10));
function sumPrimes(n)
{
var foundPrimes = 0;
var i = 2;
var sum = 0;
while(foundPrimes < n)
{
if (isPrime(i))
{
foundPrimes++;
sum += i;
}
i++;
}
return sum;
}
// Returns true if number n 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;
}
printDistances(100);
// Print distances between the first n prime numbers
function printDistances(n)
{
var lastPrime = 2;
var i = lastPrime + 1;
var foundPrimes = 1;
while(foundPrimes < n)
{
if (isPrime(i))
{
println(i - lastPrime, "\t", i, " - ", lastPrime);
foundPrimes++;
lastPrime = i;
}
i++;
}
}
// Returns true if number n 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 n1 = "29090342249";
var n2 = "29309499";
var sum = add(n1, n2);
println(n1, "\n", n2, "\n", sum);
function add(sNumber1, sNumber2)
{
var s = "";
var carry = 0;
var maxSize = Math.max(sNumber1.length, sNumber2.length);
for(var i = 0; i < maxSize; i++)
{
var digit1 = digitFromRight(sNumber1, i);
var digit2 = digitFromRight(sNumber2, i);
var sum = digit1 + digit2 + carry;
var digitSum = sum % 10;
carry = sum >= 10 ? 1 : 0;
s = digitSum.toString() + s;
}
if (carry > 0)
s = carry + s;
return s;
}
function digitFromRight(s, digitNo)
{
if (digitNo >= s.length)
return 0;
var char = s[ s.length - 1 - digitNo ];
return parseInt(char);
}
var n1 = "29090342249";
var n2 = "29309499";
var sum = add(n1, n2);
println(n1);
println(n2);
println(sum);
function add(sNumber1, sNumber2)
{
var maxSize = Math.max(sNumber1.length, sNumber2.length);
var s1 = sNumber1.padStart(maxSize, "0");
var s2 = sNumber2.padStart(maxSize, "0");
var s = "";
var carry = 0;
for(var i = maxSize - 1; i >= 0; i--)
{
var digit1 = parseInt(s1[i]);
var digit2 = parseInt(s2[i]);
var sum = digit1 + digit2 + carry;
var digitSum = sum % 10;
carry = sum >= 10 ? 1 : 0;
s = digitSum.toString() + s;
}
if (carry > 0)
s = carry + s;
return s;
}
// Solution 1
function countWords(text)
{
var wasSeparator = true;
var words = 0;
for(var c of text)
{
// if current character is separator then advance and
// set that the previous character was separator
if (isSeparator(c))
{
wasSeparator = true;
continue;
}
// if current character is not separator
// ... but if previous was separator...
if (wasSeparator)
{
words++;
wasSeparator = false;
}
}
return words;
}
function isSeparator(c)
{
var separators = [" ", "\t", "\n", "\r", ",", ";", ".", "!", "?"];
return separators.includes(c);
}
println(countWords(""));
println(countWords(" "));
println(countWords("JavaScript!!! "));
println(countWords(" JavaScript"));
println(countWords(" JavaScript is cool "));
println(countWords("I like to learn JavaScript with codeguppy"));
// Solution 2
function countWords(text)
{
var words = 0;
if (text.length > 0 && !isSeparator(text[0]))
words++;
for(var i = 1; i < text.length; i++)
{
var currChr = text[i];
var prevChr = text[i - 1];
if (!isSeparator(currChr) && isSeparator(prevChr))
{
words++;
}
}
return words;
}
function isSeparator(c)
{
var separators = [" ", "\t", "\n", "\r", ",", ";", ".", "!", "?"];
return separators.includes(c);
}
println(countWords(""));
println(countWords(" "));
println(countWords("JavaScript!!! "));
println(countWords(" JavaScript"));
println(countWords(" JavaScript is cool "));
println(countWords("I like to learn JavaScript with codeguppy"));
println(captializeWords("Create a function that will capitalize the first letter of each word in a text"));
function captializeWords(text)
{
var text2 = "";
for(var i = 0; i < text.length; i++)
{
var currChr = text[i];
var prevChr = i > 0 ? text[i - 1] : " ";
if (!isSeparator(currChr) && isSeparator(prevChr))
{
currChr = currChr.toUpperCase();
}
text2 += currChr;
}
return text2;
}
function isSeparator(c)
{
var separators = [" ", "\t", "\n", "\r", ",", ";", ".", "!", "?"];
return separators.includes(c);
}
println(sumCSV("1.5, 2.3, 3.1, 4, 5.5, 6, 7, 8, 9, 10.9"));
function sumCSV(s)
{
var ar = s.split(",");
var sum = 0;
for(var n of ar)
{
sum += parseFloat(n);
}
return sum;
}
var text = "Create a function, that will return an array (of string), with the words inside the text";
println(getWords(text));
function getWords(text)
{
let startWord = -1;
let ar = [];
for(let i = 0; i <= text.length; i++)
{
let c = i < text.length ? text[i] : " ";
if (!isSeparator(c) && startWord < 0)
{
startWord = i;
}
if (isSeparator(c) && startWord >= 0)
{
let word = text.substring(startWord, i);
ar.push(word);
startWord = -1;
}
}
return ar;
}
function isSeparator(c)
{
var separators = [" ", "\t", "\n", "\r", ",", ";", ".", "!", "?", "(", ")"];
return separators.includes(c);
}
var data = "John;Smith;954-000-0000\nMich;Tiger;305-000-0000\nMonique;Vasquez;103-000-0000";
var ar = csvToArray(data);
println(JSON.stringify(ar));
function csvToArray(data)
{
var arLines = data.split("\n");
for(var i = 0; i < arLines.length; i++)
{
var arLine = arLines[i].split(";");
arLines[i] = arLine;
}
return arLines;
}
Coding challenge #36. Create a function that converts a string to an array of characters
//codeguppy.com/code.html?XCD9vmSQ34HrascysDBL
println(getCharCodes("I like JavaScript"));
function getCharCodes(s)
{
var ar = [];
for(var i = 0; i < s.length; i++)
{
var code = s.charCodeAt(i);
ar.push(code);
}
return ar;
}
println(codesToString([73,32,108,105,107,101,32,74,97,118,97,83,99,114,105,112,116]));
function codesToString(ar)
{
return String.fromCharCode(...ar);
}
var text = "I LOVE JAVASCRIPT";
var textEnc = encrypt(text, 13);
var textDec = decrypt(textEnc, 13);
println(text);
println(textEnc);
println(textDec);
// Decrypt a message by using the same encrypt function
// ... but using the inverse of the key (e.g. rotate in the other direction)
function decrypt(msg, key)
{
return encrypt(msg, key * -1);
}
// Function will implement Caesar Cipher to
// encrypt / decrypt the msg by shifting the letters
// of the message acording to the key
function encrypt(msg, key)
{
var encMsg = "";
for(var i = 0; i < msg.length; i++)
{
var code = msg.charCodeAt(i);
// Encrypt only letters in 'A' ... 'Z' interval
if (code >= 65 && code <= 65 + 26 - 1)
{
code -= 65;
code = mod(code + key, 26);
code += 65;
}
encMsg += String.fromCharCode(code);
}
return encMsg;
}
// Modulo function: n mod p
function mod(n, p)
{
if ( n < 0 )
n = p - Math.abs(n) % p;
return n % p;
}
var ar = [23, 1000, 1, -1, 8, 3];
println(ar);
bubbleSort(ar);
println(ar);
function bubbleSort(ar)
{
var shouldSort = true;
var length = ar.length;
while(shouldSort)
{
shouldSort = false;
length--;
for(var i = 0; i < length; i++)
{
var a = ar[i];
if ( a > ar[i+1] )
{
ar[i] = ar[i+1];
ar[i+1] = a;
shouldSort = true;
}
}
}
}
println(getDistance(100, 100, 400, 300));
function getDistance(x1, y1, x2, y2)
{
var l1 = x2 - x1;
var l2 = y2 - y1;
return Math.sqrt(l1 * l1 + l2 * l2);
}
println(collisionCircleCircle(200, 200, 100, 300, 300, 50));
function collisionCircleCircle(circle1X, circle1Y, circle1R, circle2X, circle2Y, circle2R)
{
return getDistance(circle1X, circle1Y, circle2X, circle2Y) <= circle1R + circle2R;
}
// Calculate the distance between the two specified points
function getDistance(x1, y1, x2, y2)
{
var l1 = x2 - x1;
var l2 = y2 - y1;
return Math.sqrt(l1 * l1 + l2 * l2);
}
var ar = [ ["John", 120],
["Jane", 115],
["Thomas", 123],
["Mel", 112],
["Charley", 122]
];
var numbers = extractCol(ar, 1);
println(numbers);
function extractCol(ar, colNo)
{
var arCol = [];
for(var i = 0; i < ar.length; i++)
{
arCol.push(ar[i][colNo]);
}
return arCol;
}
println(binaryToNumber("11111111"));
function binaryToNumber(sBinary)
{
return parseInt(sBinary, 2);
}
var ar = [1, 2, [15, [23], [5, 12]], [100]];
println(sumArray(ar));
function sumArray(ar)
{
var sum = 0;
for(var el of ar)
{
if (Array.isArray(el))
{
el = sumArray(el);
}
sum += el;
}
return sum;
}
// Solution 1
var ar = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0];
var max = findMax(ar);
println("Max = ", max);
// Use recursion to find the maximum numeric value in an array of arrays
function findMax(ar)
{
var max = -Infinity;
// Cycle through all the elements of the array
for(var i = 0; i < ar.length; i++)
{
var el = ar[i];
// If an element is of type array then invoke the same function
// to find out the maximum element of that subarray
if ( Array.isArray(el) )
{
el = findMax( el );
}
if ( el > max )
{
max = el;
}
}
return max;
}
// Solution 2
var ar = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0];
var max = findMax(ar);
println("Max = ", max);
// Use a stack to find the maximum numeric value in an array of arrays
function findMax(arElements)
{
var max = -Infinity;
// This is the stack on which will put the first array and then
// all the other sub-arrays that we find as we traverse an array
var arrays = [];
arrays.push(arElements);
// Loop as long as are arrays added to the stack for processing
while(arrays.length > 0)
{
// Extract an array from the stack
ar = arrays.pop();
// ... and loop through its elements
for(var i = 0; i < ar.length; i++)
{
var el = ar[i];
// If an element is of type array, we'll add it to stack
// to be processed later
if ( Array.isArray(el) )
{
arrays.push(el);
continue;
}
if ( el > max )
{
max = el;
}
}
}
return max;
}
var ar1 = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0];
var ar2 = copyArray(ar1);
println(ar2);
function copyArray(ar)
{
var ar2 = [];
for(var el of ar)
{
if (Array.isArray(el))
{
el = copyArray(el);
}
ar2.push(el);
}
return ar2;
}
var text = "Create a function to return the longest word(s) in a sentance.";
println(getLongestWords(text));
function getLongestWords(text)
{
var words = getWords(text);
var maxSize = 0;
var maxPositions = [];
for(var i = 0; i < words.length; i++)
{
var currWordSize = words[i].length;
if (currWordSize > maxSize)
{
maxSize = currWordSize;
maxPositions = [ i ];
}
else if (currWordSize === maxSize)
{
maxPositions.push(i);
}
}
return getElements(words, maxPositions);
}
// Get only the elements from specified positions from the array
function getElements(ar, arPositions)
{
var arNew = [];
for(var pos of arPositions)
{
arNew.push(ar[pos]);
}
return arNew;
}
// Returns an array with the words from specified text
function getWords(text)
{
let startWord = -1;
let ar = [];
for(let i = 0; i <= text.length; i++)
{
let c = i < text.length ? text[i] : " ";
if (!isSeparator(c) && startWord < 0)
{
startWord = i;
}
if (isSeparator(c) && startWord >= 0)
{
let word = text.substring(startWord, i);
ar.push(word);
startWord = -1;
}
}
return ar;
}
function isSeparator(c)
{
var separators = [" ", "\t", "\n", "\r", ",", ";", ".", "!", "?", "(", ")"];
return separators.includes(c);
}
var ar = ["Shuffle", "an", "array", "of", "strings"];
println(shuffleArray(ar));
// Shuffle array implemented using Fisher–Yates shuffle algorithm
function shuffleArray(ar)
{
for(var i = ar.length - 1; i > 0; i--)
{
var j = randomInt(0, i - 1);
var t = ar[i];
ar[i] = ar[j];
ar[j] = t;
}
return ar;
}
// Get a random int between min and max (both included)
function randomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
println(getRandomNumbers(10));
function getRandomNumbers(n)
{
var ar = [];
for(var i = 1; i <= n; i++)
{
ar.push(i);
}
shuffleArray(ar);
return ar;
}
// Shuffle array implemented using Fisher–Yates shuffle algorithm
function shuffleArray(ar)
{
for(var i = ar.length - 1; i > 0; i--)
{
var j = randomInt(0, i - 1);
var t = ar[i];
ar[i] = ar[j];
ar[j] = t;
}
return ar;
}
// Get a random int between min and max (both included)
function randomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
var ar = getCharFrequency("Find the frequency of characters inside a string");
println(JSON.stringify(ar));
function getCharFrequency(text)
{
var ar = [];
for(var chr of text)
{
updateFrequency(ar, chr);
}
return ar;
}
function updateFrequency(ar, chr)
{
for(var el of ar)
{
if (el.chr === chr)
{
el.count++;
}
}
ar.push({ chr : chr, count : 1 });
}
println(fibonacci(500));
function fibonacci(n)
{
if (n === 0)
return "0";
if (n === 1)
return "1";
var n1 = "0";
var n2 = "1";
for(var i = 2; i <= n; i++)
{
var sum = add(n1, n2);
n1 = n2;
n2 = sum;
}
return n2;
}
function add(sNumber1, sNumber2)
{
var maxSize = Math.max(sNumber1.length, sNumber2.length);
var s1 = sNumber1.padStart(maxSize, "0");
var s2 = sNumber2.padStart(maxSize, "0");
var s = "";
var carry = 0;
for(var i = maxSize - 1; i >= 0; i--)
{
var digit1 = parseInt(s1[i]);
var digit2 = parseInt(s2[i]);
var sum = digit1 + digit2 + carry;
var digitSum = sum % 10;
carry = sum >= 10 ? 1 : 0;
s = digitSum.toString() + s;
}
if (carry > 0)
s = carry + s;
return s;
}
println(factorial(70));
// Calculate factorial(n) ... using big number calculations
function factorial(n)
{
var prod = "1";
for(var i = 2; i <= n; i++)
{
prod = mult(prod, i.toString());
}
return prod;
}
// Multiplies sNumber1 * sNumber2
// Each number is provided as string
function mult(sNumber1, sNumber2)
{
// Calculate partial results according to multiplication algorithm
var partialResults = [];
for(var i = sNumber2.length - 1; i >= 0; i--)
{
var digit = parseInt(sNumber2[i]);
var partialResult = multDigit(sNumber1, digit);
partialResult += "0".repeat(partialResults.length);
partialResults.push(partialResult);
}
// Sum partial results to obtain the product
var sum = "";
for(var r of partialResults)
{
sum = add(sum, r);
}
return sum;
}
// Multiplies number sNumber (as string) with a single digit number
function multDigit(sNumber, digit)
{
var p = "";
var carry = 0;
for(var i = sNumber.length - 1; i >= 0; i--)
{
var numberDigit = parseInt(sNumber[i]);
var prod = digit * numberDigit + carry;
var prodDigit = prod % 10;
carry = Math.floor(prod / 10);
p = prodDigit.toString() + p;
}
if (carry > 0)
p = carry + p;
return p;
}
function add(sNumber1, sNumber2)
{
var maxSize = Math.max(sNumber1.length, sNumber2.length);
var s1 = sNumber1.padStart(maxSize, "0");
var s2 = sNumber2.padStart(maxSize, "0");
var s = "";
var carry = 0;
for(var i = maxSize - 1; i >= 0; i--)
{
var digit1 = parseInt(s1[i]);
var digit2 = parseInt(s2[i]);
var sum = digit1 + digit2 + carry;
var digitSum = sum % 10;
carry = sum >= 10 ? 1 : 0;
s = digitSum.toString() + s;
}
if (carry > 0)
s = carry + s;
return s;
}
If you have a github account you can fork the entire repository with these challenges from
I hope you had fun browsing through these coding challenges. If you like them, and you want more coding challenges for beginners please leave a comment or send a tweet to .
Previously published at