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(arr) {
var longest = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i].length > longest) {
longest = arr[i].length;
}
}
return longest;
}


console.log(findLongestWord(["aa","bbb","cccc"]) === 4);
14 changes: 14 additions & 0 deletions js/isCharacterAVowel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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) {
vowels = ['a','e','i','o','u','A','E','I','O','U']
if (vowels.includes(char)) {
return true;
}
return false;
}

console.log(isCharacterAVowel("a") === true)
console.log(isCharacterAVowel("A") === true)
console.log(isCharacterAVowel("4") === false)
console.log(isCharacterAVowel(0) === false)
26 changes: 26 additions & 0 deletions js/isPrime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Create a function to return true or false if a number passed in a prime number.

function isPrime(num) {
// for special cases of 1 and 2
if (num === 1) {
return false;
}
if (num === 2) {
return true;
}
// start evaluating a number from its square to reduce number of computations
var newNum = Math.ceil(Math.sqrt(num));

// if the number is divisible by anything other than 1, it is not an integer
for (var i = newNum; i > 1; i--) {
if (Number.isInteger(num/i)) {
return false;
}
}
return true;
}

console.log(isPrime(1) === false);
console.log(isPrime(2) === true);
console.log(isPrime(541) === true);
console.log(isPrime(4) === false);
34 changes: 34 additions & 0 deletions js/letterCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Write a function that takes a string that finds out how many times a character occurs. For example, the string "apple" would print the following:

// a - 1
// p - 2
// l - 1
// e - 1
// Hint: Loop through each letter, and increment the value to a key in an object. The key should be that object.

// BONUS: Make sure that lower case letters and upper case letters count for the same character. Also, ignore spaces and punctuation.

function letterCount(word) {
var letters = {};
word = word.toLowerCase().replace(/\s/g, ''); // strip white space and lowercase letters

for (var i = 0; i < word.length; i++) {
var letter = word[i];
if (letters[letter]) {
letters[letter] += 1;
}
else {
letters[letter] = 1;
}
}
for (var letter in letters) {
console.log(letter + " - " + letters[letter]);
}
}



letterCount("apple");
letterCount("apPle");
letterCount("apple ");
letterCount("apple APPLE");
16 changes: 16 additions & 0 deletions js/maxOfThree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 bigNum = num1;
if (num2 > bigNum) {
bigNum = num2;
}
if (num3 > bigNum) {
bigNum = num3;
}
return bigNum;
}

console.log(maxOfThree(3, 1, 2))
console.log(maxOfThree(4, 15, 23))
console.log(maxOfThree(3, 100, 20))
28 changes: 28 additions & 0 deletions js/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 arr3 = [];
var totalCount = arr1.length + arr2.length;

for (var i = 0; i < totalCount; i++) {
if (arr1.length === 0) {
arr3 = arr3.concat(arr2);
break;
}
if (arr2.length === 0) {
arr3 = arr3.concat(arr1);
break;
}
var num1 = arr1[0];
var num2 = arr2[0];
if (num1 < num2) {
arr3 = arr3.concat(arr1.shift());
}
else {
arr3 = arr3.concat(arr2.shift());
}
}
return arr3;
}

console.log(merge([3,6,11],[2,4,5,8,9]))
36 changes: 36 additions & 0 deletions js/primes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Using your isPrime() function, create a function primes that will return an array of all prime numbers up to a certain amount.

function isPrime(num) {
// for special cases of 1 and 2
if (num === 1) {
return false;
}
if (num === 2) {
return true;
}
// start evaluating a number from its square to reduce number of computations
var newNum = Math.ceil(Math.sqrt(num));

// if the number is divisible by anything other than 1, it is not an integer
for (var i = newNum; i > 1; i--) {
if (Number.isInteger(num/i)) {
return false;
}
}
return true;
}

function primes(limit) {
var count = 0;
// check positive integers only
for (i = 1; i <= limit; i++) {
if (isPrime(i)) {
count++;
}
}
return count;
}

console.log(primes(10) === 4);
console.log(primes(1000) === 168);

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

function reverseString(str) {
var reversed = "";
for (var i = 0; i < str.length; i++) {
reversed += str[str.length - 1 - i];
}
return reversed;
}

console.log(reverseString("jag testar") === "ratset gaj");
12 changes: 12 additions & 0 deletions js/sillySum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Write a function that takes an array of numbers, and returns the sum of each number multiplied by its index.

function sillySum(arr) {
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i] * i;
}
return sum;
}

console.log(sillySum([2,5]) === 5);
console.log(sillySum([1,2,3]) === 8);
20 changes: 20 additions & 0 deletions js/sumArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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(arr) {
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

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

console.log(sumArray([1,2,3,4]) === 10);
console.log(multiplyArray([1,2,3,4]) === 24);