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

freecodecamp: Introduction to the JavaScript Algorithms and Data Structures Projects

freecodecamp: Introduction to the JavaScript Algorithms and Data Structures Projects

go go far away from home, coder

Palindrome Checker

"function palindrome(str) {
// Good luck!
let reg=/\W|_/ig;
let pure= str.replace(reg,'').toLowerCase();
console.log(pure);
return pure===pure.split('').reverse().join('');
}"

Roman Numeral Converter

"function convertToRoman(num) {
let mapping={
"1000" : "M",
"900" : "CM",
"500" : "D",
"400" : "CD",
"100" : "C",
"90" : "XC",
"50" : "L",
"40" : "XL",
"10" : "X",
"9" : "IX",
"5" : "V",
"4" : "IV",
"1" : "I"
};
if(typeof(num)!== 'number')
return;
let keys = Object.keys(mapping).sort((a, b) => (b - a));
let ans = "";
keys.forEach((key)=> {
while (num >= key) {
ans += mapping[key];
num -= key;
}
});
return ans;
}"

Caesars Cipher

"return str.replace(/[A-Z]/g, L => String.fromCharCode((L.charCodeAt(0) % 26) + 65));;"

Telephone Number Validator

"function telephoneCheck(str) {
var regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/; return regex.test(str); 
}"

Cash Register

"var denom = [
{ name: "ONE HUNDRED", val: 100 },
{ name: "TWENTY", val: 20 },
{ name: "TEN", val: 10 },
{ name: "FIVE", val: 5 },
{ name: "ONE", val: 1 },
{ name: "QUARTER", val: 0.25 },
{ name: "DIME", val: 0.1 },
{ name: "NICKEL", val: 0.05 },
{ name: "PENNY", val: 0.01 }
];

function checkCashRegister(price, cash, cid) {
var output = { status: null, change: [] };
var change = cash - price;
var register = cid.reduce(
function(acc, curr) {
acc.total += curr[1];
acc[curr[0]] = curr[1];
return acc;
},
{ total: 0 }
);
if (register.total === change) {
output.status = "CLOSED";
output.change = cid;
return output;
}
if (register.total < change) {
output.status = "INSUFFICIENT_FUNDS";
return output;
}
var change_arr = denom.reduce(function(acc, curr) {
var value = 0;
while (register[curr.name] > 0 && change >= curr.val) {
change -= curr.val;
register[curr.name] -= curr.val;
value += curr.val;
change = Math.round(change * 100) / 100;
}
if (value > 0) {
acc.push([curr.name, value]);
}
return acc;
}, []);
if (change_arr.length < 1 || change > 0) {
output.status = "INSUFFICIENT_FUNDS";
return output;
}
output.status = "OPEN";
output.change = change_arr;
return output;
}"


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: 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 "