简体   繁体   中英

return x doesn't work for second function

Ok so I don't know how to phrase out this question, so if you have recommendations for visibility I'm glad for tips. So now onto my problem, I'm trying to run this script I wrote in Tampermonkey but he won't return the correct answer which should be figured out in the first function. As far as I can see, the console doesn't return an error. I'm not sure what I'm doing wrong. My code currently looks like this:

 // ==UserScript== // @name Energy Air Game Bot // @namespace https://github.com/RayJW/Energy-Air-Game-Bot/edit/master/Energy%20Air%20Game%20Bot.user.js // @version 0.1 // @description Automate the Energy Air Game // @author RayJW // @match https://game.energy.ch/ // @grant none // ==/UserScript== document.getElementsByClassName("jumbotron mobile-no-padding-question").addEventListener("load", auswerten()); function Frage() { var question = document.getElementsByClassName("mobile-padding-question"); var x = 0; var questions = [ "Wie heisst der aktuelle Sommerhit von Energy Air Act Alvaro Soler?", "Auf welchem Weg kann man KEINE Energy Air Tickets gewinnen?", "Wer eröffnete das erste Energy Air?", "Wann ist der offizielle Filmstart von DAS SCHÖNSTE MÄDCHEN DER WELT in den Schweizer Kinos?", "Welche Farbe haben die Haare des Social Media Stars Julia Beautx im Film?", "Wie viele Acts waren beim letzten Energy Air dabei?", "Welcher dieser Acts hatte einen Auftritt am Energy Air 2017?", "Die wievielte Energy Air Ausgabe findet dieses Jahr statt?", "Mit welchem Preis wurde der Nachwuchsstar Luna Wedler dieses Jahr ausgezeichnet?", "Energy Air ist der einzige Energy Event,...", "Wohin führt die Klassenfahrt?", "Wann findet das Energy Air 2018 statt?", "Welcher Schauspieler/Rapper trägt im Film eine goldene Maske?", "Welche Fussballmannschaft ist im Stade de Suisse zuhause?", "Was ist Cyrils besondere Begabung?", "Wann fand Energy Air zum ersten Mal statt?", "Wer stand am letzten Energy Air als Überraschungsgast auf der Bühne?", "Wie viele Energy Air Tickets werden verlost?", "Was ist Cyrils (Aaron Hilmer) Markenzeichen im Film?", "Wann beginnt das Energy Air 2018?", "Das NRJ-Gefährt ist ein...", "Was passiert, wenn es am Eventtag regnet?", "Wo erfährst du immer die neusten Infos rund um Energy Air?", "Energy Air Tickets kann man...", "Wie schwer ist die Energy Air Bühne?", "Welcher Schweizer Shootingstar spielt in DAS SCHÖNSTE MÄDCHEN DER WELT die Hauptrolle?", "Mit welchem dieser Tickets geniesst du die beste Sicht zur Energy Air Bühne?", "Wer war der letzte Act beim Energy Air 2017?", "Wer spielt die Mutter von Cyril?", "Wo findet das Energy Air statt?", ]; while (x < 20) { if (question == questions[x]) { return x; break; } else { x++; } } } function antwort() { var answers = [ "La Cintura", "E-Mail", "Pegasus", "6. September 2018", "Pink", "15", "Aloe Blacc", "Die fünfte", "Shootingstar Berlinale 2018", "…für den man Tickets nur gewinnen kann.", "Berlin", "8. September 2018", "Cyril", "BSC Young Boys", "Texte schreiben und rappen", "2014", "Bastian Baker", "40'000", "Seine grosse Nase", "Um 16 Uhr", "Tuk Tuk", "Energy Air findet trotzdem statt", "im Radio, auf der Website und über Social Media", "gewinnen", "450 Tonnen", "Luna Wedler", "XTRA-Circle", "Kodaline", "Anke Engelke", "Stade de Suisse (Bern)" ]; var x = Frage(); window.alert(answers[x]); document.getElementById(answers[x]).checked = true; document.getElementById("next-question").click(); } function auswerten() { for (var i = 0; i < 10; i++) { antwort(); } } 

I have never coded in Javascript before so I'm not aware of any advanced technics. Everything used was researched on the go and kinda built together. If anyone knows another approach I'd be glad to try out.

The function getElementsByClassName return an array of elements and you are not accessing any specific element at the evaluation.

Here you are trying to find the question if (question == questions[x]) but as question is an array is never going to work so the function never return the index of the question you are looking for.

You can update the evaluation like if (question[x].innerHTML.trim() == questions[x])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM