Chuyển đến nội dung chính

freecodecamp: Basic algorithm Scripting

freecodecamp: Basic algorithm Scripting

note code snip to remember

Reverse a String

" str= str.split("").reverse().join("");"

Factorialize a Number

" if(num===0)
return 1;
if(num<0)
return "negative";
else
return num*factorialize(num-1);"

Find the Longest Word in a String

" let words= str.split(' ');
words.sort((a,b)=> b.length - a.length);
return words[0].length;"

 Return Largest Numbers in Arrays

" arr.map(a4=>a4.sort((a,b)=>(b-a)));
return [arr[0][0],arr[1][0],arr[2][0],arr[3][0]];"

 Confirm the Ending

let lTarget=target.length;
let r= str.slice(str.length-lTarget,str.length);
return r===target;

Repeat a String Repeat a String


"function repeatStringNumTimes(str, num) {
// repeat after me
if(num<=0)
return '';
else
{
let r='';
for(let i=0;i<num;i++)
{
r+=str;
}
return r;
}
}"

Truncate a String

" if (num<=0)
return ;
else{
return (str.length<=num) ? str : str.slice(0,num) +"...";
}"

Finders Keepers

"function findElement(arr, func ) {
return arr.filter(func)[0];
//let num = 0;
//return num;
}
findElement([1, 2, 3, 4], num => num % 2 === 0);"

Boo who


" return (typeof bool === "boolean") ? true : false;"

Title Case a Sentence


" let toL= str.toLowerCase();
let aL= toL.split(' ');
let a= aL.map(a=>a[0].toUpperCase() + a.substr(1,a.length-1));
return a.join(' ');
}"

Slice and Splice


" let arr1Cpy= arr1.slice(0,arr1.length);
let arr2Cpy= arr2.slice(0,arr2.length);
let a= arr2Cpy.slice(0,n);
let b= arr2Cpy.splice(n,arr2Cpy.length);
let c= a.concat(arr1Cpy);
console.log(a);
console.log(b);
console.log(c);
return c.concat(b) ;"

Falsy Bouncer

"return arr.filter(Boolean);"

Where do I Belong


" arr.sort((a,b)=>a-b);
let i=0;
for(i;i<arr.length;i++)
{
if(num<=arr[i])
return i;
}
return i;
}"

Mutations
"function mutation(arr) {

let desArr1= arr[0].toLowerCase().split('');
let desArr2= arr[1].toLowerCase().split('');
desArr1.sort();
desArr2.sort();
let min;
let count=0;
let maxArr=[];
let minArr=[];
let desArr=[];
if(desArr1.length>=desArr2.length)
{
minArr=desArr2.slice(0,desArr2.length);
maxArr=desArr1.slice(0,desArr1.length);
min=desArr2.length;
}
else
{
minArr=desArr1.slice(0,desArr1.length);
maxArr=desArr2.slice(0,desArr2.length);
min= desArr1.length;

}
let max=maxArr.length;;
for(let i=0;i<max;i++){
for(let j=0;j<min;j++)
{
if((maxArr[i]==minArr[j]&&maxArr[i-1]!=maxArr[i]))
desArr.push(minArr[j])
}
}
if(desArr.length==min)
return true;
else
return false;
}

mutation(["hello", "hey"]);"


Chunky Monkey

"function chunkArrayInGroups(arr, size) {
// Break it up.
if(arr.length<=size)
return ;
let l=arr.length;
let step=size;
let r=[]
let begin=0;
let end=size;
let last;
while(step<=l)
{
r.push(arr.slice(begin,end));
begin=end;
last=step;
step+=size;
end=step;
}
if(l%size!=0)
r.push(arr.slice(last,l));
return r;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);"


""

Nhận xét

Bài đăng phổ biến từ blog này

freecodecamp: React

freecodecamp: React step by step to practice Create a Simple JSX Element " const JSX = <h1>Hello JSX!</h1>; " Create a Complex JSX Element " const JSX=<div> <h1></h1> <p></p> <ul> <li></li> <li></li> <li></li> </ul> </div>; " Add Comments in JSX " const JSX = ( /* here in first time */ <div> <h1>This is a block of JSX</h1> <p>Here 's a subtitle</p> </div> ); " Render HTML Elements to the DOM " const JSX = ( <div id= 'challenge-node' > <h1>Hello World</h1> <p>Lets render this to the DOM</p> </div> ); // change code below this line ReactDOM.render(JSX,document.getElementById( 'challenge-node' )); " Define an HTML Class in JSX " cons...

freecodecamp: javascript debugging

freecodecamp: javascript debugging I note that passed code to help coder find the solution Use the JavaScript Console to Check the Value of a Variable "console.log(a);" Understanding the Differences between the freeCodeCamp and Browser Console " console.log(outputTwo); console.clear(); console.log(outputOne); " Use typeof to Check the Type of a Variable " console.log( typeof (seven)); console.log( typeof (three)); " Catch Misspelled Variable and Function Names "" //copy &past variable Catch Unclosed Parentheses, Brackets, Braces and Quotes " let myArray = [ 1 , 2 , 3 ]; let arraySum = myArray.reduce((previous, current) => previous + current); " Catch Mixed Usage of Single and Double Quotes " let innerHtml = "<p>Click here to <a href=\"#Home\">return home</a></p>" ; " Catch Use of Assignment Operator Instead of Equality Ope...

freecodecamp: basic data structures

freecodecamp: basic data structures I note passed challenge code for help. Use an Array to Store a Collection of Data " let yourArray=[ 1 , true , "3" , 4 , 5 ]; // change this line "  Access an Array's Contents Using Bracket Notation " myArray[ 1 ]= "bb" ; " Add Items to an Array with push() and unshift() " arr.unshift( "I" , 2 , "three" ); arr.push( 7 , "VIII" , 9 ); "  Remove Items from an Array with pop() and shift() " let popped=arr.pop(); // change this line let shifted=arr.shift(); // change this line " Remove Items Using splice() " // change code below this line arr.splice( 2 , 2 ); // change code above this line " Add Items Using splice() " // change code below this line arr.splice( 0 , 2 , 'DarkSalmon' , 'BlanchedAlmond' ); // change code above this line " ...