CodingChallenges
Micro Coding Challenges. Mastering these coding challenges may not get you a job at google... but you'll be one step closer to building your own JavaScript game at codeguppy.com.
Install / Use
/learn @CodeGuppyPrograms/CodingChallengesREADME
JavaScript Coding Challenges for beginners
Mastering these coding challenges may not get you a job at google... but you'll be one step closer to building your own JavaScript game at codeguppy.com.
These coding challenges are intended for beginners, therefore the solutions are implemented using only simple / classical programming elements. Each solution is acompanied by an online link that helps you quickly run it in a code playground at codeguppy.com
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 replaceprintln()withconsole.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);
Coding challenge #18: Print the first 100 prime numbers
https://codeguppy.com/code.html?gnMVeOZXN6VhLekyvui8
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;
}
Coding challenge #19: Create a function that will return in an array the first "nPrimes" prime numbers greater than a particular number "startAt"
https://codeguppy.com/code.html?mTi7EdKrviwIn4bfrmM7
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;
}
Coding challenge #20: Rotate an array to the left 1 position
https://codeguppy.com/code.html?MRmfvuQdZpHn0k03hITn
var ar = [1, 2, 3];
rotateLeft(ar);
println(ar);
function rotateLeft(ar)
{
var first = ar.shift();
ar.push(first);
}
Coding challenge #21: Rotate an array to the right 1 position
https://codeguppy.com/code.html?fHfZqUmkAVUXKtRupmzZ
var ar = [1, 2, 3];
rotateRight(ar);
println(ar);
function rotateRight(ar)
{
var last = ar.pop();
ar.unshift(last);
}
Coding challenge #22: Reverse an array
https://codeguppy.com/code.html?GZddBqBVFlqYrsxi3Vbu
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;
}
Coding challenge #23: Reverse a string
https://codeguppy.com/code.html?pGpyBz0dWlsj7KR3WnFF
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;
}
Coding challenge #24: Create a function that will merge two arrays and return the result as a new array
Related Skills
node-connect
353.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
353.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
353.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
