Skip to content

Instantly share code, notes, and snippets.

View JacquesPoulin's full-sized avatar
🌐
Coding ...

Jacques Poulin JacquesPoulin

🌐
Coding ...
View GitHub Profile
@JacquesPoulin
JacquesPoulin / countLetters.js
Last active March 30, 2022 15:43
### ... Return object where letters are keys and occurrences of those letters are values.
const countLetters = (str) => {
// convert the string to an array
return [...str].reduce(
(acc, curr) =>
// check if character has been seen before
acc.hasOwnProperty(curr)
? { ...acc, [curr]: acc[curr] + 1 } // + 1
: { ...acc, [curr]: 1 }, // add it to the object with a count of 1
{} // start with an empty object
);
@JacquesPoulin
JacquesPoulin / reset.css
Created March 9, 2022 23:03
* Modern CSS Reset *
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Remove default margin */
body,
h1,
@JacquesPoulin
JacquesPoulin / RandomColor.js
Last active February 20, 2022 18:43
# Générer des couleurs au hasard
const generateRandomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16)}`;
@JacquesPoulin
JacquesPoulin / ShuffleArray.js
Last active February 20, 2022 18:44
# Retourner un tableau au hasard à chaque appel de fonction
const shuffleArray = (arr) => arr.sort(() => Math.random() - 0.5);
// Testing
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(shuffleArray(arr)); // OUTPUT : [ 10, 6, 2, 4, 9, 1, 3, 7, 5, 8 ]
@JacquesPoulin
JacquesPoulin / UniqueElement.js
Last active February 20, 2022 18:42
# Retourner un tableau avec un seul élément de chaque
const getUnique = (arr) => [...new Set(arr)];
const arr = [1, 1, 2, 3, 3, 4, 4, 4, 5, 5];
console.log(getUnique(arr)) // >>> OUTPUT : [ 1, 2, 3, 4, 5 ]
@JacquesPoulin
JacquesPoulin / GitStart
Last active February 19, 2022 18:06
# Résumé des étapes principales pour GIT
1. Créer un dossier et se placer dedans
2. Cliquer dans la barre de tâche windows du dossier (elle devient bleue)
3. Taper "cmd" puis "enter"
4. Dans le Shell :
> commencer un GIT : "git init"
> Ajouter : "git add ."
> Vérifier que tout est ok : "git status"
> Faire un commit (ne pas oublier le message) : 'git commit -m "ici le message"'
> Une fois le message écrit on tape sur "enter"
> Pour voir tous les commit : "git log"
@JacquesPoulin
JacquesPoulin / FindReplace.js
Last active February 19, 2022 14:41
## Effectuer une recherche et un remplacement sur la phrase en utilisant les arguments fournis et renvoyez la nouvelle phrase.
// SOLUTION 1
function myReplace(str, before, after) {
// Find index where before is on string
var index = str.indexOf(before);
// Check to see if the first letter is uppercase or not
if (str[index] === str[index].toUpperCase()) {
// Change the after word to be capitalized before we use it.
after = after.charAt(0).toUpperCase() + after.slice(1);
} else {
// Change the after word to be uncapitalized before we use it.
@JacquesPoulin
JacquesPoulin / MatchKeyValue.js
Last active February 17, 2022 23:49
### Parcourir un tableau d'objets et renvoyer un tableau de tous les objets dont les paires nom-valeur correspondent
function whatIsInAName(collection, source) {
let srcKeys = Object.keys(source);
return collection.filter((obj) => srcKeys.every((key) => obj.hasOwnProperty(key) && obj[key] === source[key]));
// On filtre la collection en utilisant ".filter()"
// Ensuite, on retourne une valeur booléenne pour la méthode ".filter()"
// Enfin, on réduit la valeur booléenne à renvoyer pour la méthode ".every()"
};
@JacquesPoulin
JacquesPoulin / SortAlpha.js
Last active February 14, 2022 22:30
# Trier un tableau dans l'ordre alphabétique
function alphabeticalOrder(arr) {
return arr.sort((a, b) => a === b ? 0 : a > b ? 1 : - 1);
};
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
@JacquesPoulin
JacquesPoulin / CountStrings.js
Last active February 19, 2022 14:44
# Comment compter le nombre de lettres identiques dans un tableau ? (peu importe la casse)
function count(str) {
// On met tout en minuscule
str = str.toLowerCase();
// On met tout dans un tableau
let arr = str.split('');
// On compte la lettre souhaitée (par ex. le "A") :
let count_A = arr.reduce((n, val) => {