Skip to content

Instantly share code, notes, and snippets.

@KimSarabia
KimSarabia / dowhile.js
Created November 17, 2015 08:19
do while in an array
var userChoice = "";
do
{
var targetNumber = Number(prompt("Please enter your target number", ""));
var start = 0;
while (start <= targetNumber)
{
document.write(start + "<br/>");
start = start + 2;
}
@KimSarabia
KimSarabia / swapCase.js
Created November 17, 2015 11:22
CH Challenge #5
var swapCase = function(string){
var newString = " ";
for(var i = 0; i< string.length; i++){
if(string[i] === string[i].toLowerCase()){
newString += string[i].toUpperCase();
}else {
newString += string[i].toLowerCase();
}
}
console.log(newString);
@KimSarabia
KimSarabia / triangular.js
Created November 17, 2015 21:29
Triangular Number
function triangular(N) {
var total = 0;
for(var i = 1; i <= N; i++){
total += i;
}
return total;
}
console.log(triangular(100));
//5050
@KimSarabia
KimSarabia / isEven-recursion.js
Last active November 22, 2015 01:33
Eloquent JavaScript Chapter 3 Q.2
/*Recursion
We’ve seen that % (the remainder operator) can be used to test whether a number
is even or odd by using % 2 to check whether it’s divisible by two.
Here’s another way to define whether a positive whole number is even or odd:
Zero is even.
One is odd.
For any other number N, its evenness is the same as N - 2.
Define a recursive function isEven corresponding to this description.
The function should accept a number parameter and return a Boolean.
Test it on 50 and 75. See how it behaves on -1. Why? Can you think of a way to fix this?
@KimSarabia
KimSarabia / gist:6d54de3e24d83bc540af
Created November 22, 2015 03:52 — forked from lsauer/gist:2757250
JavaScript : within a string, count the number of occurances of a character / character counting and string-position
//www.lsauer.com 2012
//Answer to:
//http://stackoverflow.com/questions/881085/count-the-number-of-occurances-of-a-character-in-a-string-in-javascript/10671743#10671743
//There are at least four ways. The best option, which should also be the fastest -owing to the native RegEx engine -, is placed at the top. //jsperf.com is currently down, otherwise I would provide you with performance statistics.
#1.
("this is foo bar".match(/o/g)||[]).length
//>2
#2.
"this is foo bar".split("o").length-1
@KimSarabia
KimSarabia / beanCounting.js
Last active November 22, 2015 05:04
Eloquent JavaScript Chapter 3 Question 3
/*You can get the Nth character, or letter, from a string by writing "string".charAt(N)
similar to how you get its length with "s".length.
The returned value will be a string containing only one character (for example, "b").
The first character has position zero, which causes the last one to be found at position string.length - 1.
In other words, a two-character string has length 2, and its characters have positions 0 and 1.
Write a function countBs that takes a string as its only argument
Returns a number that indicates how many uppercase “B” characters are in the string.
Next, write a function called countChar that behaves like countBs,
except it takes a second argument that indicates the character that is to be counted
(rather than counting only uppercase “B” characters). Rewrite countBs to make use of this new function.
@KimSarabia
KimSarabia / index.html
Created March 1, 2016 06:34
the Movie Database API (Television)
<div class='search'>
<input type="text" id='query' required></input>
<button id='search'>Search</button>
</div>
<div class="movieOptions">
<button class="categories" onclick="topMovies()">Top Movies</button>
<button class="categories" onclick="popular()">Popular</button>
<button class="categories" onclick="nowPlaying()"> Now Playing </button>
<button class="categories" onclick="upcoming()"> Upcoming </button>
<button class="categories" onclick="tvPopular()"> TV Popular </button>
@KimSarabia
KimSarabia / validate_credit_card.js
Created March 14, 2016 23:57 — forked from DiegoSalazar/validate_credit_card.js
Luhn algorithm in Javascript. Check valid credit card numbers
// takes the form field value and returns true on valid number
function valid_credit_card(value) {
// accept only digits, dashes or spaces
if (/[^0-9-\s]+/.test(value)) return false;
// The Luhn Algorithm. It's so pretty.
var nCheck = 0, nDigit = 0, bEven = false;
value = value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {
@KimSarabia
KimSarabia / README.md
Last active March 24, 2016 18:38 — forked from zenorocha/README.md
A template for Github READMEs (Markdown) + Sublime Snippet

Project Name

TODO: Write a project description

Installation

TODO: Describe the installation process

Usage

@KimSarabia
KimSarabia / object-comparison.js
Created September 29, 2017 14:24
Object Comparison
//Source: https://stackoverflow.com/questions/33232823/javascript-compare-two-objects-and-get-key-value-pair
// compare objects with symmetrical keys
function diffObject(a, b) {
return Object.keys(a).reduce(function(map, k) {
if (a[k] !== b[k]) map[k] = b[k];
return map;
}, {});
}
console.log(diffObject({A: 10, B: 20, C: 30}, {A: 10, B: 22, C: 30}));