Skip to content

Instantly share code, notes, and snippets.

@anotherjung
Created May 29, 2015 21:54
Show Gist options
  • Save anotherjung/2e645a313b1811d1dd38 to your computer and use it in GitHub Desktop.
Save anotherjung/2e645a313b1811d1dd38 to your computer and use it in GitHub Desktop.
Basic Algorithms // source http://jsbin.com/juqoti
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic Algorithms</title>
</head>
<body>
<script id="jsbin-javascript">
//#1 (Print 1-255) Write a program that would print all the numbers from 1 to 255.
/**
for(var i=1; i<256; i++) {console.log(i);}
**/
//#2(Odd Numbers) Write a program that would print all the odd numbers from 1 to 1000
/**
for (var i=0; i<=1000; i++)
{
if (i%2 !==0)
console.log(i);
}
**/
//#3 (Print Sum) Write a program that would print the sum of all the odd numbers from 1 to 5000
/**
var sum = 0;
for(var i=1; i<=5000; i=i+2)
{
sum = sum +i;
}
console.log(sum);
**/
//#4 (Iterating through the array) Given an array X say [1,3,5,7,9,13], write a program that would iterate through each member of the array and print each value on the screen. Being able to loop through each member of the array is extremely important. Do this over and over (under 2 minutes) before moving on to the next algorithm challenge.
/**
x=[1,3,5,7,9,13];
for(var i=0; i<x.length; i++)
{
console.log(x[i]);
}
**/
//#5 (Find Max) Given an array with multiple values (e.g. [-3, 3, 5, 7]), write a program that prints the maximum number in the array. (The best way to do this is to have the computer go through each number, one at a time, and to update the value in a variable called 'maximum' (or whatever you want to name the variable); imagine that if I gave you no number and asked you what a maximum number is. What would you say? Say the first number I gave you was -3 and asked you what a maximum number is. What would you say? Say the next number I gave you was 3 and asked you again what a maximum number now is. What would you say? Have the computer imitate this behavior of updating the maximum number as you iterate through each number in the array). Again you're not to use any of the pre-built functions
/**
x=[-3, 3, 5, 7];
var max = x[0];
for(var i=1; i<x.length; i++)
{
if (x[i]>max)
{
max = x[i];
}
}
console.log(max);
**/
//#6 (Find Average) Given an array with multiple values (e.g. [1,3,5,7,20]), write a program that prints the average of the values in the array. Again you're not to do this by using of any of the pre-built functions in Javascript. Again iterate through each number in the array and update the 'average' as the program retrieves each number in the array.
/**
x=[1, -3, 50, 7];
var sum = 0;
for(var i=0; i<x.length; i++)
{
sum = sum + x[i];
}
var average = sum / x.length;
console.log(average);
**/
//#7 (Array with odd numbers) Write a program that creates an array 'Y' that contains all the odd numbers between 1 to 255. When the program is done, 'y' should have the value of [1, 3, 5, 7, ... 255]. Again, make sure you come up with a simple base case and write instructions to solve that base case first and then test your instructions for other complicated cases. (you can do this using a simple for loop. You are allowed to use .push method)
/**
var y = [];
for(var i=1; i<256; i++)
{
y.push(i);
}
console.log(y);
**/
//#8 (Greater than Y) Write a program that takes an array and returns the number of values in that array whose value is greater than y. For example if array = [1,3, 5, 7] and y = 3, after your program is run it will print 2 (since there are two values in the array whose value is greater than 3). Again make sure you come up with a simple base case and write instructions to solve that base case first and then test your instructions for other complicated cases. You can use a count function with this assignment.
/**
var x=[1, 3, 5, 7];
var y=3;
var count = 0;
for(var i=0; i<x.length; i++)
{
if (x[i]>y) //if the new x-value greater than y
{
count++;
}
}
console.log(count);
**/
//#9 (Square the values) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that squares each value in the array. When the program is done x should have values that have been squared (e.g. [1, 25, 100, 4]). You're not to use any of the pre-built function in Javascript. You could for example square the value by saying x[i] = x[i] * x[i];
/**
var x=[1,5, 10, -2];
for(var i=0;i<x.length; i++)
{
x[i] = x[i] * x[i];
}
console.log(x);
**/
//#10 (Eliminate Negative Numbers) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that replaces any negative number with the value of 0. When the program is done x should have no negative values (e.g. [1, 5, 10, 0]).
/**
x=[1,5,10,-2];
for(var i=0;i<x.length; i++)
{
if (x[i] < 0)
{
x=0;
}
}
**/
//#11 (Max, Min, and Average) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that prints the maximum number in the array, minimum value in the array as well as the average values in the array.
/**
x=[1,5,10,-2];
var max = x[0];
var min = x[0];
var sum = 0;
sum = sum+x[0];
for (var i=1; i<x.length;i++)
{
if (x[i]>max)
{
max = x[i];
}
if (x[i]<min)
{
min = x[i];
}
sum = sum + x[i];
}
console.log('Max is ',max);
console.log('Min is ',min);
console.log('Avg is ',sum/x.length);
**/
//#12 (Shifting the values in the array) Given an array x (e.g. [1,5, 10, 7, -2]), create an algorithm (sets of instructions) that shifts each number by one (to the front). For example when the program is done x (assuming it was [1,5,10,7,-2]) should become [5,10,7,-2, 0]. Make sure you understand how to swap values (see Sample 15 on the previous tab).
/**
x=[1,5,10,7,-2];
console.log(x);
for (var i=0; i<x.length-1; i++)
{
x[i] = x[i+1];
}
x[x.length-1]=0; //sets the last number in the arry to zero
console.log(x);
**/
//#13 (Number to String) Write a program that takes an array of numbers and replaces any number that's negative to a string called 'Dojo'. For example if array = [-1, -3, 2] after your program is done array should be ['Dojo', 'Dojo', 2].
x=[-1,-3,2];
for (var i=0;i<x.length;i++)
{
if (x[i]< 0) {
x[i] = 'Dojo';
}
}
console.log(x);
</script>
<script id="jsbin-source-javascript" type="text/javascript">//#1 (Print 1-255) Write a program that would print all the numbers from 1 to 255.
/**
for(var i=1; i<256; i++) {console.log(i);}
**/
//#2(Odd Numbers) Write a program that would print all the odd numbers from 1 to 1000
/**
for (var i=0; i<=1000; i++)
{
if (i%2 !==0)
console.log(i);
}
**/
//#3 (Print Sum) Write a program that would print the sum of all the odd numbers from 1 to 5000
/**
var sum = 0;
for(var i=1; i<=5000; i=i+2)
{
sum = sum +i;
}
console.log(sum);
**/
//#4 (Iterating through the array) Given an array X say [1,3,5,7,9,13], write a program that would iterate through each member of the array and print each value on the screen. Being able to loop through each member of the array is extremely important. Do this over and over (under 2 minutes) before moving on to the next algorithm challenge.
/**
x=[1,3,5,7,9,13];
for(var i=0; i<x.length; i++)
{
console.log(x[i]);
}
**/
//#5 (Find Max) Given an array with multiple values (e.g. [-3, 3, 5, 7]), write a program that prints the maximum number in the array. (The best way to do this is to have the computer go through each number, one at a time, and to update the value in a variable called 'maximum' (or whatever you want to name the variable); imagine that if I gave you no number and asked you what a maximum number is. What would you say? Say the first number I gave you was -3 and asked you what a maximum number is. What would you say? Say the next number I gave you was 3 and asked you again what a maximum number now is. What would you say? Have the computer imitate this behavior of updating the maximum number as you iterate through each number in the array). Again you're not to use any of the pre-built functions
/**
x=[-3, 3, 5, 7];
var max = x[0];
for(var i=1; i<x.length; i++)
{
if (x[i]>max)
{
max = x[i];
}
}
console.log(max);
**/
//#6 (Find Average) Given an array with multiple values (e.g. [1,3,5,7,20]), write a program that prints the average of the values in the array. Again you're not to do this by using of any of the pre-built functions in Javascript. Again iterate through each number in the array and update the 'average' as the program retrieves each number in the array.
/**
x=[1, -3, 50, 7];
var sum = 0;
for(var i=0; i<x.length; i++)
{
sum = sum + x[i];
}
var average = sum / x.length;
console.log(average);
**/
//#7 (Array with odd numbers) Write a program that creates an array 'Y' that contains all the odd numbers between 1 to 255. When the program is done, 'y' should have the value of [1, 3, 5, 7, ... 255]. Again, make sure you come up with a simple base case and write instructions to solve that base case first and then test your instructions for other complicated cases. (you can do this using a simple for loop. You are allowed to use .push method)
/**
var y = [];
for(var i=1; i<256; i++)
{
y.push(i);
}
console.log(y);
**/
//#8 (Greater than Y) Write a program that takes an array and returns the number of values in that array whose value is greater than y. For example if array = [1,3, 5, 7] and y = 3, after your program is run it will print 2 (since there are two values in the array whose value is greater than 3). Again make sure you come up with a simple base case and write instructions to solve that base case first and then test your instructions for other complicated cases. You can use a count function with this assignment.
/**
var x=[1, 3, 5, 7];
var y=3;
var count = 0;
for(var i=0; i<x.length; i++)
{
if (x[i]>y) //if the new x-value greater than y
{
count++;
}
}
console.log(count);
**/
//#9 (Square the values) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that squares each value in the array. When the program is done x should have values that have been squared (e.g. [1, 25, 100, 4]). You're not to use any of the pre-built function in Javascript. You could for example square the value by saying x[i] = x[i] * x[i];
/**
var x=[1,5, 10, -2];
for(var i=0;i<x.length; i++)
{
x[i] = x[i] * x[i];
}
console.log(x);
**/
//#10 (Eliminate Negative Numbers) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that replaces any negative number with the value of 0. When the program is done x should have no negative values (e.g. [1, 5, 10, 0]).
/**
x=[1,5,10,-2];
for(var i=0;i<x.length; i++)
{
if (x[i] < 0)
{
x=0;
}
}
**/
//#11 (Max, Min, and Average) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that prints the maximum number in the array, minimum value in the array as well as the average values in the array.
/**
x=[1,5,10,-2];
var max = x[0];
var min = x[0];
var sum = 0;
sum = sum+x[0];
for (var i=1; i<x.length;i++)
{
if (x[i]>max)
{
max = x[i];
}
if (x[i]<min)
{
min = x[i];
}
sum = sum + x[i];
}
console.log('Max is ',max);
console.log('Min is ',min);
console.log('Avg is ',sum/x.length);
**/
//#12 (Shifting the values in the array) Given an array x (e.g. [1,5, 10, 7, -2]), create an algorithm (sets of instructions) that shifts each number by one (to the front). For example when the program is done x (assuming it was [1,5,10,7,-2]) should become [5,10,7,-2, 0]. Make sure you understand how to swap values (see Sample 15 on the previous tab).
/**
x=[1,5,10,7,-2];
console.log(x);
for (var i=0; i<x.length-1; i++)
{
x[i] = x[i+1];
}
x[x.length-1]=0; //sets the last number in the arry to zero
console.log(x);
**/
//#13 (Number to String) Write a program that takes an array of numbers and replaces any number that's negative to a string called 'Dojo'. For example if array = [-1, -3, 2] after your program is done array should be ['Dojo', 'Dojo', 2].
x=[-1,-3,2];
for (var i=0;i<x.length;i++)
{
if (x[i]< 0) {
x[i] = 'Dojo';
}
}
console.log(x);
</script></body>
</html>
//#1 (Print 1-255) Write a program that would print all the numbers from 1 to 255.
/**
for(var i=1; i<256; i++) {console.log(i);}
**/
//#2(Odd Numbers) Write a program that would print all the odd numbers from 1 to 1000
/**
for (var i=0; i<=1000; i++)
{
if (i%2 !==0)
console.log(i);
}
**/
//#3 (Print Sum) Write a program that would print the sum of all the odd numbers from 1 to 5000
/**
var sum = 0;
for(var i=1; i<=5000; i=i+2)
{
sum = sum +i;
}
console.log(sum);
**/
//#4 (Iterating through the array) Given an array X say [1,3,5,7,9,13], write a program that would iterate through each member of the array and print each value on the screen. Being able to loop through each member of the array is extremely important. Do this over and over (under 2 minutes) before moving on to the next algorithm challenge.
/**
x=[1,3,5,7,9,13];
for(var i=0; i<x.length; i++)
{
console.log(x[i]);
}
**/
//#5 (Find Max) Given an array with multiple values (e.g. [-3, 3, 5, 7]), write a program that prints the maximum number in the array. (The best way to do this is to have the computer go through each number, one at a time, and to update the value in a variable called 'maximum' (or whatever you want to name the variable); imagine that if I gave you no number and asked you what a maximum number is. What would you say? Say the first number I gave you was -3 and asked you what a maximum number is. What would you say? Say the next number I gave you was 3 and asked you again what a maximum number now is. What would you say? Have the computer imitate this behavior of updating the maximum number as you iterate through each number in the array). Again you're not to use any of the pre-built functions
/**
x=[-3, 3, 5, 7];
var max = x[0];
for(var i=1; i<x.length; i++)
{
if (x[i]>max)
{
max = x[i];
}
}
console.log(max);
**/
//#6 (Find Average) Given an array with multiple values (e.g. [1,3,5,7,20]), write a program that prints the average of the values in the array. Again you're not to do this by using of any of the pre-built functions in Javascript. Again iterate through each number in the array and update the 'average' as the program retrieves each number in the array.
/**
x=[1, -3, 50, 7];
var sum = 0;
for(var i=0; i<x.length; i++)
{
sum = sum + x[i];
}
var average = sum / x.length;
console.log(average);
**/
//#7 (Array with odd numbers) Write a program that creates an array 'Y' that contains all the odd numbers between 1 to 255. When the program is done, 'y' should have the value of [1, 3, 5, 7, ... 255]. Again, make sure you come up with a simple base case and write instructions to solve that base case first and then test your instructions for other complicated cases. (you can do this using a simple for loop. You are allowed to use .push method)
/**
var y = [];
for(var i=1; i<256; i++)
{
y.push(i);
}
console.log(y);
**/
//#8 (Greater than Y) Write a program that takes an array and returns the number of values in that array whose value is greater than y. For example if array = [1,3, 5, 7] and y = 3, after your program is run it will print 2 (since there are two values in the array whose value is greater than 3). Again make sure you come up with a simple base case and write instructions to solve that base case first and then test your instructions for other complicated cases. You can use a count function with this assignment.
/**
var x=[1, 3, 5, 7];
var y=3;
var count = 0;
for(var i=0; i<x.length; i++)
{
if (x[i]>y) //if the new x-value greater than y
{
count++;
}
}
console.log(count);
**/
//#9 (Square the values) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that squares each value in the array. When the program is done x should have values that have been squared (e.g. [1, 25, 100, 4]). You're not to use any of the pre-built function in Javascript. You could for example square the value by saying x[i] = x[i] * x[i];
/**
var x=[1,5, 10, -2];
for(var i=0;i<x.length; i++)
{
x[i] = x[i] * x[i];
}
console.log(x);
**/
//#10 (Eliminate Negative Numbers) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that replaces any negative number with the value of 0. When the program is done x should have no negative values (e.g. [1, 5, 10, 0]).
/**
x=[1,5,10,-2];
for(var i=0;i<x.length; i++)
{
if (x[i] < 0)
{
x=0;
}
}
**/
//#11 (Max, Min, and Average) Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that prints the maximum number in the array, minimum value in the array as well as the average values in the array.
/**
x=[1,5,10,-2];
var max = x[0];
var min = x[0];
var sum = 0;
sum = sum+x[0];
for (var i=1; i<x.length;i++)
{
if (x[i]>max)
{
max = x[i];
}
if (x[i]<min)
{
min = x[i];
}
sum = sum + x[i];
}
console.log('Max is ',max);
console.log('Min is ',min);
console.log('Avg is ',sum/x.length);
**/
//#12 (Shifting the values in the array) Given an array x (e.g. [1,5, 10, 7, -2]), create an algorithm (sets of instructions) that shifts each number by one (to the front). For example when the program is done x (assuming it was [1,5,10,7,-2]) should become [5,10,7,-2, 0]. Make sure you understand how to swap values (see Sample 15 on the previous tab).
/**
x=[1,5,10,7,-2];
console.log(x);
for (var i=0; i<x.length-1; i++)
{
x[i] = x[i+1];
}
x[x.length-1]=0; //sets the last number in the arry to zero
console.log(x);
**/
//#13 (Number to String) Write a program that takes an array of numbers and replaces any number that's negative to a string called 'Dojo'. For example if array = [-1, -3, 2] after your program is done array should be ['Dojo', 'Dojo', 2].
x=[-1,-3,2];
for (var i=0;i<x.length;i++)
{
if (x[i]< 0) {
x[i] = 'Dojo';
}
}
console.log(x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment