Skip to content

Instantly share code, notes, and snippets.

@NATASHA-ct
Created July 20, 2022 15:28
Show Gist options
  • Select an option

  • Save NATASHA-ct/ca65b75178490bf68b9436e0c63e5bda to your computer and use it in GitHub Desktop.

Select an option

Save NATASHA-ct/ca65b75178490bf68b9436e0c63e5bda to your computer and use it in GitHub Desktop.
DRY code, to keep it clean, beautiful, understandable and maintainable.
const pets = ['Cat', 'Dog', 'Bird', 'Fish', 'Frog', 'Hamster', 'Pig', 'Horse', 'Lion', 'Dragon'];
// Print all pets
console.log(pets[0]);
console.log(pets[1]);
console.log(pets[2]);
console.log(pets[3]);
...
.cat {
font-family: "Times New Roman", Times, serif;
font-size: 1rem;
color: #FFF;
}
.dog {
font-family: "Times New Roman", Times, serif;
font-size: 1rem;
color: #000;
}
.dragon {
font-family: "Times New Roman", Times, serif;
font-size: 1rem;
color: #009933;
}
................................................................................
#The code above is not dry because it has repetition when trying to console every animal object.
//A For loop can be used instead.
const pets = ['Cat', 'Dog', 'Bird', 'Fish', 'Frog', 'Hamster', 'Pig', 'Horse', 'Lion', 'Dragon'];
pets.sort();
pets.reverse();
let newPets = "";
for (i=0; i < pets.length; i++) {
newPets += pets[i] + "<br>";
}
const greet = (message, name) => {
console.log(`${message}, ${name}!`)
}
greet('Hello', 'John');
greet('Hola', 'Antonio');
greet('Ciao', 'Luigi')
// output............
.greetings {
font-family: Arial, sans-serif;
font-size: 1.5rem;
}
.greetings.english {
background-color: #000;
color: #FFF;
}
.greetings.spanish {
background-color: #FFF;
color: #000;
}
//this code looks dry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment