mirror of
https://github.com/LucasVbr/nuitdelinfo-2023.git
synced 2026-05-13 17:21:56 +00:00
nouvelle fonction correcte
This commit is contained in:
Vendored
-3
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"liveServer.settings.port": 5501
|
||||
}
|
||||
+13
-2
@@ -156,8 +156,19 @@ function getImg2(categorie, indice){
|
||||
// function for random index of a function
|
||||
|
||||
|
||||
function getIndex(){
|
||||
|
||||
function getIndex(tableauDentiers){
|
||||
if (tableauDentiers.length == 0){
|
||||
return -1;
|
||||
}
|
||||
const indiceAleatoire = Math.floor(Math.random() * tableauDentiers.length);
|
||||
tableauDentiers.splice(indiceAleatoire, 1);
|
||||
console.log(tableauDentiers); // Affiche : [1, 2, 4, 5]
|
||||
return indiceAleatoire;
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', ()=>
|
||||
{
|
||||
const tableauDentiers = [1, 2, 3, 4, 5];
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -6,6 +6,6 @@
|
||||
<script src="fonctions.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<button onclick="getImg1('Commerce',0)">Salut!</button>
|
||||
<button onclick="getIndex()">Salut!</button>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,89 +0,0 @@
|
||||
|
||||
function fonction_marrante() {// Charger le contenu du fichier JSON
|
||||
fetch('./resources/Commerce.json')
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`Erreur de chargement du fichier JSON : ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(objetJson => {
|
||||
// Accéder au tableau 'questions' dans l'objet JSON
|
||||
const questions = objetJson.questions;
|
||||
|
||||
// Afficher chaque question dans la console
|
||||
questions.forEach(question => {
|
||||
console.log(question.question);
|
||||
});
|
||||
|
||||
alert('Questions chargées avec succès. Consultez la console pour les détails.');
|
||||
})
|
||||
.catch(erreur => {
|
||||
console.error('Erreur lors du chargement du fichier JSON :', erreur.message);
|
||||
alert('Erreur lors du chargement du fichier JSON. Consultez la console pour les détails.');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// retourne random index
|
||||
|
||||
async function getQuestionByCategorie2(category) {
|
||||
try {
|
||||
const response = await fetch(`./${category}.json`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Erreur de chargement du fichier JSON : ${response.status}`);
|
||||
}
|
||||
|
||||
const objetJson = await response.json();
|
||||
const questions = objetJson.questions;
|
||||
|
||||
console.log(questions);
|
||||
return questions;
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur lors du chargement du fichier JSON :', error.message);
|
||||
alert('Erreur lors du chargement du fichier JSON. Consultez la console pour les détails.');
|
||||
throw error; // Re-throw the error for further handling
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
console.log(getQuestionByCategorie2("transport"))
|
||||
|
||||
function shuffleArray(array) {
|
||||
const shuffledArray = [...array];
|
||||
for (let i = shuffledArray.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
|
||||
}
|
||||
return shuffledArray;
|
||||
}
|
||||
|
||||
function createShuffledIndicesArray(array) {
|
||||
const indicesArray = Array.from({ length: array.length }, (_, index) => index);
|
||||
return shuffleArray(indicesArray);
|
||||
}
|
||||
|
||||
function getNextIndex(indicesArray) {
|
||||
if (indicesArray.length === 0) {
|
||||
// Si tous les indices ont été utilisés, réinitialiser le tableau d'indices
|
||||
return null; // ou une autre valeur pour indiquer que tous les indices ont été utilisés
|
||||
}
|
||||
// Retourner et retirer le premier indice du tableau
|
||||
return indicesArray.shift();
|
||||
}
|
||||
|
||||
// Utilisez ces fonctions comme suit :
|
||||
|
||||
// const votreTableau = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"];
|
||||
const votreTableau = await getQuestionByCategorie2('transport');
|
||||
const shuffledIndices = createShuffledIndicesArray(votreTableau);
|
||||
|
||||
// À chaque appel, récupérez le prochain indice
|
||||
const premierIndice = getNextIndex(shuffledIndices);
|
||||
const deuxiemeIndice = getNextIndex(shuffledIndices);
|
||||
|
||||
console.log(premierIndice); // Indice aléatoire
|
||||
console.log(deuxiemeIndice); // Indice différent
|
||||
|
||||
Generated
-5
@@ -1,5 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
Generated
-7
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="ASK" />
|
||||
<option name="description" value="" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
-12
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Generated
-6
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="MarkdownSettingsMigration">
|
||||
<option name="stateVersion" value="1" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/front.iml" filepath="$PROJECT_DIR$/.idea/front.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
-6
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
Reference in New Issue
Block a user