Skip to content

added homework #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions isPrime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var isPrime = function(n){
if (n === 1) {return false;}
else if (n === 2) { return true;}
else if (n === 3) { return true;}
else {
for(var i = Math.floor(Math.sqrt(n)); i>=2; i--){
if(n%i ==0 || n%2 == 0 || n%3 == 0) {return false;}
}
}
return true;
};

console.log(isPrime(7));
37 changes: 37 additions & 0 deletions letterCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** 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
BONUS: Make sure that lower case letters and upper case letters count for
the same character. Also, ignore spaces and punctuation.

Notes: strings can be accessed like arrays (but you cant change them
that way b/c all strings are constant)

using objects here makes sense because want to associate a value (the letter) with
its key (the count that the letter occurs)

if (character in letterCount) {letterCount[character]+=1}else{
letterCount[character] = 1
}
**/

var letterCount = function(str) {
var obj = {};
for (var i = 0; i<str.length; i+=1){
var letter = str[i];
if(letter in obj) {
obj[letter] += 1;
}
else {
obj[letter]=1; // here is where a letter is initially added to the object as a key and 1 is put in as the value
}
}
return obj;
};

console.log(letterCount("hello,world"));
10 changes: 10 additions & 0 deletions numSquare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

var numSquare = function(maxNum){
var perSquare = [];
for(var i=0; (i*i)<maxNum; i++) {
perSquare.push(i*i)
};
return perSquare;
}

console.log(numSquare(100));
11 changes: 11 additions & 0 deletions sillySum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var array = [1,2,3,45];


var sillySum = function(taco){
var count = 0;
for (var i=0; i<taco.length; i++) {
count = count + (taco[i] * i);
}
return(count);
}
console.log(sillySum(array));