Adding some small code challenges + some interesting functions

This commit is contained in:
gauvainboiche
2025-07-11 23:44:51 +02:00
parent 548360c345
commit abe38843b3
4 changed files with 108 additions and 2 deletions

View File

@@ -0,0 +1,43 @@
// Write your code here:
function dogFactory(name, breed, weight) {
if (typeof name !== "string") {
return "Please provide a valid string for the NAME parameter.";
} else if (typeof breed !== "string") {
return "Please provide a valid string for the BREED parameter.";
} else if (typeof weight !== "number") {
return "Please provide a valid number for the WEIGHT parameter.";
} else {
return {
_name: name,
_breed: breed,
_weight: weight,
// GETTER methods
get name() { return this._name; },
get breed() { return this._breed; },
get weight() { return this._weight; },
// SETTER methods
set name(newName) { this._name = newName; },
set breed(newBreed) { this._breed = newBreed; },
set weight(newWeight) { this._weight = newWeight; },
// ADDITIONNAL methods
bark() { return(`ruff! ruff!`); },
eatTooManyTreats() { this._weight++; }
};
}
}
const barney = dogFactory("Barney", "Bastard", 34);
// Print after creation
console.log(`Name > ${barney.name}
Breed > ${barney.breed}
Weight > ${barney.weight}
--------------------`);
// Name change and he ate too much !
barney.name = "Barnaby";
barney.eatTooManyTreats();
// Print of the changes
console.log(`Name > ${barney.name}
Breed > ${barney.breed}
Weight > ${barney.weight}
--------------------`);

View File

@@ -88,7 +88,7 @@ console.log(god.battles);
*/ */
const godNames = god.gods.map(g => g.name); const godNames = god.gods.map(god => god.name);
const godDomains = god.gods.map(g => g.domain); const godDomains = god.gods.map(god => god.domain);
console.log(godNames); console.log(godNames);
console.log(godDomains); console.log(godDomains);

View File

@@ -0,0 +1,47 @@
let story = 'Last weekend, I took literally the most beautifull bike ride of my life. The route is called "The 9W to Nyack" and it stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it literally took me an entire day. I stopped at Riverbank State Park to take some artsy photos. It was a short stop, though, because I had a freaking long way to go. After a quick photo op at the very popular Little Red Lighthouse I began my trek across the George Washington Bridge into New Jersey. The GW is a breathtaking 4,760 feet long! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautifull park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you literally cross back into New York! At this point, you are very close to the end.';
let storyWords = story.split(' ');
let unnecessaryWord = 'literally';
let misspelledWord = 'beautifull';
let badWord = 'freaking';
//console.log(storyWords); // see the results of the new array created by .split()
//console.log(storyWords.join(" ")); // see the results of joining words again
// counting initial words in the debut string
let count = 0;
storyWords.forEach(word => count++);
//console.log(count);
// applying a filter to opt out "literraly"
storyWords = storyWords.filter(word => word !== unnecessaryWord);
// applying a filter to correct any mispelled word
storyWords = storyWords.map(word => {
if (word === misspelledWord) {
return "beautiful";
} else {
return word;
}});
// applying a multi-method filter to snap any bad word
const badWordIndex = storyWords.findIndex(word => word === badWord);
//console.log(badWordIndex); // get the index position
//console.log(storyWords[78]); // check the index position
storyWords[badWordIndex] = "really"; // replace the bad word
//console.log(storyWords[78]); // check the new index position
// applying a simplification method to the text for stupid people
const lengthCheck = storyWords.every(word => word.length <= 10);
//console.log(lengthCheck);
storyWords = storyWords.map(word => {
const replacingWord = "stunning";
if (word.length > 10) {
console.log(`----- The word ${word} will be replaced by ${replacingWord} -----
`);
return replacingWord;
} else {
return word;
}});
console.log(storyWords.join(" "));

View File

@@ -0,0 +1,16 @@
// Write your code here:
// the method .reverse() does it but is NOT allowed here
function reverseArray(array) {
let newArray = [];
for (const element of array) {
newArray.unshift(element); // this will put every read element FIRST in the array
}
return newArray;
}
const sentence = ['sense.','make', 'all', 'will', 'This'];
console.log(reverseArray(sentence))
// Should print ['This', 'will', 'all', 'make', 'sense.'];