Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions js/findLongestWord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
Write a function findLongestWord that takes an array of words and returns the length of the longest word in the array.
*/

function findLongestWord(wordsArray){
var maxLength = 0;
for(var i=0;i<wordsArray.length;i++){
var currLength = wordsArray[i].length;
if(currLength>maxLength){
maxLength=currLength;
}
}
return maxLength;
}
12 changes: 12 additions & 0 deletions js/isCharacterAVowel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
Write a function isCharacterAVowel that takes a character (i.e. a string of length 1) and returns true if it is a vowel and false, otherwise.
*/

function isCharacterAVowel(char){
if(char=="a"||char=="e"||char=="i"||char=="o"||char=="u"){
console.log(true);
}
else{
console.log(false);
}
}
21 changes: 21 additions & 0 deletions js/isPrime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
Create a function to return true or false if a number passed in a prime number.
*/

function isPrime(num){
//prime numbers are greater than 1
if(num>1){
for(var i=2;i<num;i++){
if((num%i)==0){
console.log(false);
break;
}
else{
console.log(true);
}
}
}
else{
console.log(false);
}
}
25 changes: 25 additions & 0 deletions js/letterCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
Write a function that takes a string that finds out how many times a character occurs.
*/

function letterCount(word){
//Convert the word to lowercase and remove any punctuations or wide spaces
var wordModified = word.toLowerCase().replace(/\s[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");

//split the words into individual characters
var chars = word.split("");

//Adding characters and their frequencies into an object letterCount
var letterCount = {};
for(var i=0;i<chars.length;i++){
if(letterCount[chars[i]]==undefined){
letterCount[chars[i]]=0;
}
letterCount[chars[i]]+=1;
}

//iterate over letterCount and print out each letter and its frequency
for (var letter in letterCount){
console.log(letter+": "+letterCount[letter])
}
}
21 changes: 21 additions & 0 deletions js/maxOfThree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
Define a function maxOfThree that takes three numbers as arguments and returns the largest of them. Use the if-then-else construct available in Javascript.
*/


function maxOfThree(num1,num2,num3){
var max = 0;
if(num1<num2){
max = num2;
if(num2<num3){
max = num3;
}
}
else{
max = num1;
if(num1<num3){
max = num3;
}
}
return max;
}
24 changes: 24 additions & 0 deletions js/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
Write a function called merge. The function should take two sorted arrays of numbers as input and return a merged array of the sorted numbers from the input. For example, if the input arrays were var arr1 = [3,6,11]; var arr2 = [2,4,5,8,9]; Then the returned array would be: [2,3,4,5,6,8,9,11].
*/

function merge(arr1,arr2){
var bigArray=[];
addBig(arr1,bigArray);
addBig(arr2,bigArray);
console.log("Before sorting: " + bigArray);
bigArray.sort(sortNumber);
console.log("After sorting: " + bigArray);
}

function addBig(arr,bigArray){
for(i=0;i<arr.length;i++){
bigArray.push(arr[i])
}
return bigArray;
}

//by default, sort is only by alphabetically. Add this function for numerical sorting
function sortNumber(a,b){
return a - b;
}
Empty file removed js/numSquare.js
Empty file.
34 changes: 34 additions & 0 deletions js/primes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
Using your isPrime() function, create a function primes that will return an array of all prime numbers up to a certain amount.
*/

function primes(max){
var i = 0;
var array = [];
while(i<=max){
if(isPrime(i)==true){
array.push(i);
}
i++;
}
console.log(array);
}

function isPrime(num){
//prime numbers are greater than 1
if(num>1){
for(var i=2;i<num;i++){
if((num%i)==0){
return false;
break;
}
else{
return true;
}
}
}
else{
return false;
}
}

14 changes: 14 additions & 0 deletions js/reverseString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
Define a function reverseString that reverses a string. For example, reverseString("jag testar") should return the string "ratset gaj".
*/

function reverseString(inputString){
//create a new string
var newStr = "";

for(var i=inputStr.length-1;i>=0;i--){
//Starting point of loop corresponds to the last character
newStr += inputString[i];
}
return console.log(newString);
}
13 changes: 13 additions & 0 deletions js/sillySum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
Write a function that takes an array of numbers, and returns the sum of each number multiplied by its index
*/

function sillySum(array){
var output = 0;
var currSum = 0;
for(var i=0;i<array.length;i++){
currSum = array[i]*array.indexOf(array[i]);
output += currSum;
}
return output;
}
19 changes: 19 additions & 0 deletions js/sumArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
Define a function sumArray and a function multiplyArray that sums and multiplies (respectively) all the numbers in an array of numbers. For example, sumArray([1,2,3,4]) should return 10, and multiplyArray([1,2,3,4]) should return 24.
*/

function sumArray(array){
var output = 0;
for(var i=0;i<array.length;i++){
output+=array[i];
}
return output;
}

function multiplyArray(array){
var output = 1;
for (var i=0;i<array.length;i++) {
output*=array[i];
}
return output;
}