Skip to content

Instantly share code, notes, and snippets.

View Philip803's full-sized avatar

Ka Sing Leung Philip803

View GitHub Profile
/*
Find the longest single-word palindrome within a phrase.
The phrase will only contain letters (no symbols, punctuation, or numbers).
Your palindrome detection should be case-insensitive.
If there are multiple longest palindromes of equal length, return the last one.
*/
function findLongestPalindrome(sentence) {
// split sentence into words
var b = []
var a = sentence.split(/\s/)
/*
Flesh out the implementation and test it.
Implementation of WHAT, you say? What's the problem statement???
Well, you should be able to tell what this code is intended to do just from reading the starter "skeleton".
Assuming you find the above statement to be true upon reading the outline, well, then that illustrates the power of good outlining. You don't need a bunch of comments explaining the code. The code is effectively SELF-EXPLANATORY, even at this early, not-fully-implemented stage. This is the level of clarity that you should aim for in your own coding too.
===
//001
function transformFirstAndLast(array) {
//your code here
var a = {}
a[array[0]] = array[array.length - 1 ]
return a
}
//002
@Philip803
Philip803 / Fib.js
Created January 12, 2018 19:23
Fib created by PhilipLeung803 - https://repl.it/@PhilipLeung803/Fib
function fib(n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
// 1 0 | 1 1 2 3 5 8 13 21 34 55 89
@Philip803
Philip803 / Display unique number.js
Created January 12, 2018 18:39
Display unique number created by PhilipLeung803 - https://repl.it/@PhilipLeung803/Display-unique-number
var c = [1,2,1,3,6,3,6,4,4,5]
var f = c.filter(function(value,index,array){
return array.lastIndexOf(value) == index
})
var unique = c.filter((v, i, a) => a.indexOf(v) === i);
console.log(f)
@Philip803
Philip803 / Unique in Order.js
Created January 12, 2018 18:15
Unique in Order created by PhilipLeung803 - https://repl.it/@PhilipLeung803/Unique-in-Order
var uniqueInOrder=function(iterable){
//your code here - remember iterable can be a string or an array
var a = []
for(let i = 1; i<=iterable.length;i++){
if(iterable[i]!=iterable[i-1]){
a.push(iterable[i-1])
}
}
return a;
}
@Philip803
Philip803 / Who Like This.js
Created January 12, 2018 17:33
Who Like This created by PhilipLeung803 - https://repl.it/@PhilipLeung803/Who-Like-This
function likes(names) {
// TODO
if(names.length == 0) return "no one likes this"
if(names.length == 1) return `${names[0]} likes this`
if(names.length == 2) return `${names[0]} and ${names[1]} like this`
if(names.length == 3) return `${names[0]}, ${names[1]} and ${names[2]} like this`
if(names.length > 3) return `${names[0]}, ${names[1]} and ${names.length - 2} others like this`
}
@Philip803
Philip803 / Find the unique number.js
Created January 11, 2018 20:10
Find the unique number created by PhilipLeung803 - https://repl.it/@PhilipLeung803/Find-the-unique-number
function findUniq(arr) {
var answer = 0;
arr.forEach(function(each){
if(arr.indexOf(each) == arr.lastIndexOf(each)){
answer = each;
return
}
})
return answer
}
@Philip803
Philip803 / Find smallest number.js
Created January 11, 2018 19:57
Find smallest number created by PhilipLeung803 - https://repl.it/@PhilipLeung803/Find-smallest-number
class SmallestIntegerFinder {
findSmallestInt(args) {
return args.sort((a,b) => a-b)[0]
}
}
// Math.min(...args)
// Math.min(null,args)
var a = [3,45,6,7,8,922,2,3]
function checkNumbers(array){
var counter = 1;
var a = []
for(var i = 1; i <= array.length; i++){
if(array[i] != array[i-1]){
a.push("Number: " + [array[i-1], " Count: " + counter])
counter = 1;
} else {
counter++;