JS Cardio Session Two
JS exercises
by trentHarlem
HTML
<header>
<h1>
<span>/*</span> JS Cardio <span>*/</span>
</h1>
</header>
<section>
<h2>
Session Two =>
</h2>
<div>
<ul>
<li> Longest Word </li>
<li> Array Chunking </li>
<li> R3v3rs3 1nt3g3r </li>
<li> Flatten Arrays </li>
<li> Anagram Checker </li>
</ul>
</div>
</section>
CSS
body {
background-color: blueviolet;
color: whitesmoke;
font: bold 1.2em system-ui, sans-serif;
text-align: center;
}
header {
position: relative;
margin: 5px;
}
h1,
h2 {
-webkit-text-stroke: 2px black;
-webkit-text-fill-color: blueviolet;
}
h2 {
font-size: 1.9em;
margin: 40px 0px 10px 0px;
-webkit-text-fill-color: #33333399;
}
header h1 {
position: relative;
/* color: yellow; */
-webkit-text-fill-color: blueviolet;
-webkit-text-stroke: 1px blanchedalmond;
border: 1px solid chartreuse;
border-radius: 32px;
background: #ffffff33;
margin: 0;
padding: 12px;
}
header h1 span {
font-family: monospace;
-webkit-text-fill-color: blueviolet;
}
/* to center bullets */
ul {
display: inline-block;
list-style-type: circle;
text-align: left;
}
JavaScript
// Longest word in a sentence
function longestWord(sent) {
const wordArr = sent.split(' ').sort((a, b) => b.length - a.length).filter((w, _, arr) => w.length === arr[0].length)
return wordArr.length === 1 ? wordArr.join('') : wordArr
}
console.log(longestWord('i love javascript'))
console.log(longestWord('javascript love typescript'))
// Array Chunking
// split array into chunked arrays of a specific length
// ------- solution while loop WORKING
function chunkArray(arr, len) {
let output = []
while (arr.length > 0) {
let chunk = arr.splice(0, len)
output.push(chunk)
}
return output
}
// ------- solution with higher order?? W>I<P<
/* function chunkArray(arr, len) {
let output = []
//while (arr.length > 0) {
return arr.map(v=> arr.push(arr.splice(0, len)))
//output.push(chunk)
// }
//return output
} */
console.log(chunkArray([1, 2, 3, 4, 5, 6, 7, 8, 9], 3))
console.log(chunkArray([1, 2, 3, 4, 5, 6, 7], 3))
//console.log(chunkArray([1, 2, 3, 4, 5, 6, 7], 2))
// reverse Integer
// return parseInt of int convert to string, to array; reverse, join reversed array back to string, then * Math.sign(int) to handle negative int input
function revInt(int) {
return parseInt(int.toString().split('').reverse().join('')) * Math.sign(int)
}
console.log(revInt(51502112))
console.log(revInt(-12345))
/////
// flatten arrays
const flattenMe = [[1, 2],[3, 4],[5, 6],[7]]
const flattenMe2 = [['Rick', 'Sanchez'],['Morty', 'Smith'],['Butch', 'Mac Gillacutty']]
/* function flatArray(arr){
return arr.reduce((a,b)=>a.concat(b))
} */
// will Only work with NON-INTEGER
const flatArray2=(arr)=>arr.join().split(",")
//es6 flat()
function flatArray(arr) {
return arr.flat()
}
console.log('flat', flatArray(flattenMe))
console.log('flat', flatArray(flattenMe2))
////
// is Anagram ?
function isAnagram(str1, str2) {
//console.log(str2.split('').sort((a,b)=>a-b))
return str1.split('').sort().join('') == str2.split('').sort().join('') ? true :...