Last active
November 20, 2023 13:07
-
-
Save palaashatri/63cea17aa67536ea96795308a502b899 to your computer and use it in GitHub Desktop.
This file contains 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
function allEven(arr){ | |
// check if all are even numbers | |
return arr.every(num => { | |
return num%2 === 0; | |
} | |
); | |
} | |
// alt, check if all are even | |
const allEvens = (arr) => {return arr.every(num => {return num%2===0;});}; | |
console.log(allEvens([2,4,6,8])) //true | |
console.log(allEvens([1,4,6,8])) //false | |
console.log(allEvens([1,2,3])) //false | |
// check if some are even | |
const someEvens = arr => {return arr.some(num => {return num%2 === 0;});} | |
console.log(allEvens([2,4,6,8])) //true, cause [2,4,6,8] | |
console.log(allEvens([1,4,6,8])) //true, cause [4,6,8] | |
console.log(allEvens([1,2,3])) //true, cause [2] |
This file contains 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
function rollDie(sides) { | |
return Math.floor(Math.random() * sides) + 1); | |
} | |
console.log(rollDie(6)) // something between 1 -> 6 | |
console.log(rollDie()) // NaN | |
// old fix, 'check undefined' | |
function rollDieOld(sides) { | |
if (sides === undefined) { | |
sides = 6 ; // default sides is 6 | |
} | |
return Math.floor(Math.random() * sides) + 1); | |
} | |
console.log(rollDieOld(6)) // something between 1 -> 6 | |
console.log(rollDieOld()) // something between 1 -> 6 | |
// new fix, using default value of sides = 6 | |
function rollDieNew(sides = 6) { | |
return Math.floor(Math.random() * sides) + 1); | |
} | |
console.log(rollDieOld(6)) // something between 1 -> 6 | |
console.log(rollDieOld()) // something between 1 -> 6 | |
// also | |
function greet(salutation="Hello", name="World"){ | |
return `${salutation} ${name}`; | |
} | |
console.log(greet("Afternoon","Traveller")); // "Afternoon Traveller" | |
console.log(greet()); // "Hello World" |
This file contains 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
// functions inside objects are called methods | |
const square = { | |
area : function(side){ | |
return side * side; | |
}, | |
perimeter(side){ | |
return side * 4; | |
} | |
} | |
console.log(square.area(10)) //100 | |
console.log(square.perimeter(10)) //40 |
This file contains 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
// DO NOT ALTER THE FOLLOWING CODE: | |
const fullNames = [{first: 'Albus', last: 'Dumbledore'}, {first: 'Harry', last: 'Potter'}, {first: 'Hermione', last: 'Granger'}, {first: 'Ron', last: 'Weasley'}, {first: 'Rubeus', last: 'Hagrid'}, {first: 'Minerva', last: 'McGonagall'}, {first: 'Severus', last: 'Snape'}]; | |
// Write your code here | |
let firstNames = fullNames.map(function(fullName){ | |
return fullName.first; | |
}); | |
console.log(firstNames); // ['Albus', 'Harry', 'Hermione', 'Ron', 'Rubeus', 'Minerva', 'Severus'] |
This file contains 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
function validUserNames(usernames) { | |
// get usernames with less than 10 characters | |
return usernames.filter((username) => username.length < 10 ); | |
} | |
console.log(validUserNames(['mark', 'staceysmom1978', 'q29832128238983', 'carrie98', 'MoanaFan'])); | |
// or | |
const validUserNamesAlt = usernames => { | |
return usernames.filter((username) => username.length < 10 ); | |
}; | |
console.log(validUserNamesAlt(['mark', 'staceysmom1978', 'q29832128238983', 'carrie98', 'MoanaFan'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment