• Nov 19, 2022 •CodeCatch
0 likes • 2 views
function formatDate(date) { return date.toLocaleDateString(); } function Comment(props) { return ( <div className="Comment"> <div className="UserInfo"> <img className="Avatar" src={props.author.avatarUrl} alt={props.author.name} /> <div className="UserInfo-name"> {props.author.name} </div> </div> <div className="Comment-text">{props.text}</div> <div className="Comment-date"> {formatDate(props.date)} </div> </div> ); } const comment = { date: new Date(), text: 'I hope you enjoy learning React!', author: { name: 'Hello Kitty', avatarUrl: 'https://placekitten.com/g/64/64', }, }; ReactDOM.render( <Comment date={comment.date} text={comment.text} author={comment.author} />, document.getElementById('root') );
0 likes • 1 view
//JavaScript program to swap two variables //take input from the users let a = prompt('Enter the first variable: '); let b = prompt('Enter the second variable: '); // XOR operator a = a ^ b b = a ^ b a = a ^ b console.log(`The value of a after swapping: ${a}`); console.log(`The value of b after swapping: ${b}`);
• Jan 26, 2023 •AustinLeath
0 likes • 5 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
const longestPalindrome = (s) => { if(s.length === 1) return s const odd = longestOddPalindrome(s) const even = longestEvenPalindrome(s) if(odd.length >= even.length) return odd if(even.length > odd.length) return even }; const longestOddPalindrome = (s) => { let longest = 0 let longestSubStr = "" for(let i = 1; i < s.length; i++) { let currentLongest = 1 let currentLongestSubStr = "" let left = i - 1 let right = i + 1 while(left >= 0 && right < s.length) { const leftLetter = s[left] const rightLetter = s[right] left-- right++ if(leftLetter === rightLetter) { currentLongest += 2 currentLongestSubStr = s.slice(left+1, right) } else break } if(currentLongest > longest) { if(currentLongestSubStr) longestSubStr = currentLongestSubStr else longestSubStr = s[i] longest = currentLongest } } return longestSubStr } const longestEvenPalindrome = (s) => { let longest = 0 let longestSubStr = "" for(let i = 0; i < s.length - 1; i++) { if(s[i] !== s[i+1]) continue; let currentLongest = 2 let currentLongestSubStr = "" let left = i - 1 let right = i + 2 while(left >= 0 && right < s.length) { const leftLetter = s[left] const rightLetter = s[right] left-- right++ if(leftLetter === rightLetter) { currentLongest += 2 currentLongestSubStr = s.slice(left+1, right) } else break } if(currentLongest > longest) { if(currentLongestSubStr) longestSubStr = currentLongestSubStr else longestSubStr = s[i] + s[i+1] longest = currentLongest } } return longestSubStr } console.log(longestPalindrome("babad"))
• Mar 11, 2021 •C S
1 like • 1 view
if (process.env.NODE_ENV === "production") { app.use(express.static("client/build")); app.get("*", (req, res) => { res.sendFile(path.resolve(__dirname, "client", "build", "index.html")); }); }
0 likes • 4 views
// Time Complexity : O(N) // Space Complexity : O(1) var isMonotonic = function(nums) { let isMono = null; for(let i = 1; i < nums.length; i++) { if(isMono === null) { if(nums[i - 1] < nums[i]) isMono = 0; else if(nums[i - 1] > nums[i]) isMono = 1; continue; } if(nums[i - 1] < nums[i] && isMono !== 0) { return false; } else if(nums[i - 1] > nums[i] && isMono !== 1) { return false; } } return true; }; let nums1 = [1,2,2,3] let nums2 = [6,5,4,4] let nums3 = [1,3,2] console.log(isMonotonic(nums1)); console.log(isMonotonic(nums2)); console.log(isMonotonic(nums3));