Last active
January 20, 2019 01:43
-
-
Save KimTrijnh/7a29febfc94558c6465ecbaf568a5d9d to your computer and use it in GitHub Desktop.
JS hints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. function (let PROP in OBJECT) { do something} | |
// if OBJECT is a STR => ex: let str = 'abc' | |
// str is consider as Object str = {0: 'a', 1: 'b', 2:'c'} | |
//Reverse a String | |
function reverseString(str) { | |
let rStr =''; | |
for (let i in str) { | |
rStr += str[str.length-1-i] | |
} | |
return rStr; | |
} | |
reverseString("hello"); // 'olleh' | |
//Finding the longest word in a string | |
function findLongestWordLength(str) { | |
let arr = str.split(' '); | |
let newArr = []; | |
for(let i=0;i<arr.length;i++) { | |
newArr.push(arr[i].length); | |
} | |
let longest = Math.max(...newArr); | |
return longest; | |
} | |
findLongestWordLength("The quick brown fox jumped over the lazy dog"); | |
//Return largest numbers in an array | |
function largestOfFour(arr) { | |
// You can do this! | |
let newArr = []; | |
for(let i=0;i<arr.length; i++) { | |
newArr.push(Math.max(...arr[i])); | |
} | |
return newArr; | |
} | |
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); | |
//Confirm the Ending can be solved with the .endsWith() method | |
function confirmEnding(str, target) { | |
// "Never give up and good luck will find you." | |
// -- Falcor | |
if(str.slice(str.length-target.length) === target) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
confirmEnding("Bastian", "n"); | |
//Truncate a String | |
function truncateString(str, num) { | |
// Clear out that junk in your trunk | |
let newStr = ''; | |
if(str.length>num){ | |
newStr = `${str.slice(0,num)}...`; | |
} else { | |
newStr = str; | |
} | |
return newStr; | |
} | |
truncateString("A-tisket a-tasket A green and yellow basket", 8) | |
//Boo Who | |
function booWho(bool) { | |
// What is the new fad diet for ghost developers? The Boolean. | |
if(bool === true | bool === false) {return true} | |
else {return false} | |
} | |
booWho(null); | |
//Title Case a Sentence | |
//Note: .toUpperCase & .toLowerCase & str[0] Chỉ return 1 value, nhưng không thay đổi original string. | |
function titleCase(str) { | |
let arr = str.split(' '); | |
for(let i=0;i<arr.length;i++){ | |
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1).toLowerCase(); | |
} | |
let newStr = arr.join(' '); | |
return newStr; | |
} | |
titleCase("I'm a little tea pot"); | |
//slice & splice; | |
function frankenSplice(arr1, arr2, n) { | |
// It's alive. It's alive! | |
return arr2.slice(0,n).concat(arr1,arr2.slice(n)); | |
} | |
frankenSplice([1, 2, 3], [4, 5, 6], 1); | |
//Falsy Bouncer | |
function bouncer(arr) { | |
// Don't show a false ID to this bouncer. | |
/* let newArr = []; | |
for(let i in arr) { | |
if(Boolean(arr[i]) === true) { | |
newArr.push(arr[i]); | |
} | |
} */ | |
function checkBoolean(a) { | |
return Boolean(a) ===true; | |
} | |
return arr.filter(checkBoolean); | |
} | |
bouncer([7, "ate", "", false, 9]); | |
//Where do I belong | |
function getIndexToIns(arr, num) { | |
// Find my place in this sorted array. | |
arr.push(num); | |
arr.sort(function(a,b){return a-b}); | |
return arr.indexOf(num); | |
} | |
getIndexToIns([40, 60], 50); | |
//Mutations | |
function mutation(arr) { | |
let b=true; | |
for (let i in arr[1]){ | |
if(arr[0].toLowerCase().search(arr[1][i].toLowerCase()) === -1) | |
{b= false; break;} | |
else {b= true}; | |
} | |
return b; | |
} | |
mutation(["hello", "hey"]); | |
//Chunky Monkey | |
function chunkArrayInGroups(arr, size) { | |
// Break it up. | |
let newArr= []; | |
let b = arr.length/size +1; | |
for(let i=1; i< b; i++) { | |
newArr.push(arr.splice(0,size)); | |
} | |
return newArr; | |
} | |
chunkArrayInGroups(["a", "b", "c", "d"], 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Intermediate Agrothism
Make Person