📅  最后修改于: 2023-12-03 15:03:48.682000             🧑  作者: Mango
"Portez ce vieux 威士忌 au juge blond qui fume" is a famous pangram in French which means "Take this old whiskey to the blond judge who smokes". As a programmer, what can we do with this sentence? In this article, we will explore some possible ways to use this sentence with Javascript.
A pangram is a sentence that contains all letters of the alphabet at least once. We can use the sentence "Portez ce vieux 威士忌 au juge blond qui fume" to check if a string is a pangram in French. Here is the Javascript code:
function isPangram(str) {
return new Set([...str.replace(/[^a-z]/gi, '')]).size === 26;
}
const sentence = "Portez ce vieux 威士忌 au juge blond qui fume";
console.log(isPangram(sentence)); // output: true
We remove all non-alphabetical characters and convert the remaining characters to a Set to count the number of unique letters. If the size of the Set is 26, the string is a pangram.
We can create a fun drinking game with the sentence "Portez ce vieux 威士忌 au juge blond qui fume". Every player takes turns to say one letter of the sentence. Whoever fails to remember the next letter or makes a mistake has to drink. Here is the Javascript code to implement the game:
const sentence = "Portez ce vieux 威士忌 au juge blond qui fume";
const letters = sentence.replace(/[^a-z]/gi, '').split('');
let playerIndex = 0;
let nextLetterIndex = 0;
function nextPlayer() {
playerIndex = (playerIndex + 1) % players.length;
}
function nextLetter() {
console.log(`${players[playerIndex]}: ${letters[nextLetterIndex]}`);
nextLetterIndex++;
if (nextLetterIndex === letters.length) {
nextLetterIndex = 0;
nextPlayer();
}
}
const players = ['Player 1', 'Player 2', 'Player 3'];
nextLetter(); // start the game
We can also generate a random sentence based on the letters of "Portez ce vieux 威士忌 au juge blond qui fume". Here is the Javascript code to shuffle the letters and create a random sentence:
function shuffle(str) {
return str.split('').sort(() => Math.random() - 0.5).join('');
}
const sentence = "Portez ce vieux 威士忌 au juge blond qui fume";
const letters = sentence.replace(/[^a-z]/gi, '');
const randomLetters = shuffle(letters);
const randomSentence = sentence.replace(/[a-z]/gi, () => randomLetters.charAt(Math.floor(Math.random() * randomLetters.length)));
console.log(randomSentence); // output: "aepicq iu h bdjnçzfklmotrwvys xguv铁 oศm "
We shuffle the letters with the shuffle
function which uses the Fisher-Yates algorithm. Then, we replace each letter of the original sentence with a randomly selected letter from the shuffled letters.
"Portez ce vieux 威士忌 au juge blond qui fume" is not just a funny sentence in French. It can also inspire us to create interesting programs with Javascript. We have seen how to check if a string is a pangram, create a drinking game, and generate a random sentence using the letters of this sentence.