Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Vanilla Javascript使用REST国家API JSON数据生成多项选择题测验。随机排列答案顺序_Javascript_Json_Api - Fatal编程技术网

使用Vanilla Javascript使用REST国家API JSON数据生成多项选择题测验。随机排列答案顺序

使用Vanilla Javascript使用REST国家API JSON数据生成多项选择题测验。随机排列答案顺序,javascript,json,api,Javascript,Json,Api,我正在尝试使用谷歌地图API和其他国家API构建一个地理游戏/测验。我的谷歌地图功能与REST Countries API一起工作。发生的是,在每次页面刷新时,随机加载一个国家,并从REST Countries API生成数据(它是隐藏的,因此用户只会得到关于它是哪个国家的提示)。地图也会平移到随机国家的中心(仅混合图像,没有国家标签)。这正是我想要的 在我的Javascript中有一个循环用于选择题部分,但它只是从一开始就开始。有没有办法将标签随机化并将正确答案(countryData.nam

我正在尝试使用谷歌地图API和其他国家API构建一个地理游戏/测验。我的谷歌地图功能与REST Countries API一起工作。发生的是,在每次页面刷新时,随机加载一个国家,并从REST Countries API生成数据(它是隐藏的,因此用户只会得到关于它是哪个国家的提示)。地图也会平移到随机国家的中心(仅混合图像,没有国家标签)。这正是我想要的

在我的Javascript中有一个循环用于选择题部分,但它只是从一开始就开始。有没有办法将标签随机化并将正确答案(countryData.name)插入其中?我有这个工作,但我的代码不是干的,第一个选择总是正确的答案。也不知道如何让循环随机抽取答案。这是我开始设计和修复点系统之前需要做的最后一件事。抱歉,我不得不隐藏我的谷歌API密钥

const countriesList=document.getElementById(“国家”);
让gameTitle=document.querySelector(“.gameTitle”);
让我们选择一个国家;
让updateScore=document.querySelector(“.score”);
让猜测=document.querySelector(“.ErrorAnswers”);
分数=0;
让错误答案=0;
//其他国家
函数newQuestion(){
取回(“https://restcountries.eu/rest/v2/all")
.then(response=>response.json())
.然后(数据=>初始化(数据))
.catch(error=>console.log(“error:,error));
函数初始化(countriesData){
//界定国家
国家=国家数据;
//创建一个空字符串,在其中随机添加选项值作为字符串
让选项=”;
countries.forEach(country=>options+=`${country.name}`);
countriesList.innerHTML=选项;
//API调用的一个实例的随机索引
countriesList.selectedIndex=Math.floor(Math.random()*countriesList.length);
//设置或返回所选值的索引
//用于显示卡
显示CountryInfo(countriesList[countriesList.selectedIndex].value);
//显示CountryInfo(CountryList.value);
}
功能显示CountryInfo(countryByAlpha3Code){
让addQuestions=document.querySelector(“.question”);
const countryData=countries.find(country=>country.alpha3Code===countryByAlpha3Code);
selectedCountry=countryData.name;
document.querySelector(“#flag container img”).src=countryData.flag;
//如果无法加载国旗,则显示国家名称
document.querySelector(“#flag container img”).alt=`flag of${countryData.name}`;
document.getElementById(“country”).innerHTML=countryData.name;
document.getElementById(“大写”).innerHTML=countryData.capital;
document.getElementById(“population”).innerHTML=countryData.population.toLocaleString(“en-US”);
document.getElementById(“currences”).innerHTML=countryData.currences.filter(c=>c.name).map(c=>`${c.name}(${c.code})`).join(',');
让languages=document.getElementById(“languages”).innerHTML=countryData.languages.filter(l=>l.name).map(l=>`${l.name}`)。join(',');
document.getElementById(“region”).innerHTML=countryData.region;
document.getElementById(“Subsection”).innerHTML=countryData.Subsection;
document.getElementById(“lat long”).innerHTML=countryData.latlng;
initMap(countryData);
addQuestions.innerHTML=`我位于${countryData.Subsection}。这里有${countryData.languages.length}语言:${languages}。我的首都是${countryData.capital}。我叫什么名字?`;
函数multipleChoice(){
for(设i=0;i<7;i++){
让$input=document.querySelector(“#inputs”);
$input.innerHTML=$input.innerHTML+`${countries[i].name}`;
}
}
多重回声();
}
//访问谷歌地图API
功能初始化图(国家/地区){
//创建一个变量
让myLatLng=new google.maps.LatLng(country.LatLng[0],country.LatLng[1]);
//对象文字
//使用mapOptions对象实例化地图
设mapOptions={
中心:myLatLng,
缩放:5,
disableDefaultUI:true,
mapTypeId:'卫星',
标题:90,
倾斜:45,
旋转控制:对,
}
让marker=new google.maps.marker({
位置:myLatLng
});
//创建地图
让map=new google.maps.map(document.getElementById(“mapDiv”),mapOptions);
//设置标记
marker.setMap(map);
}
}
新问题();
函数getValue(元素){
如果(element.value==selectedCountry){
分数++;
updateScore.innerHTML=`Score:${Score}`;
新问题();
如果(分数=10){
gameTitle.innerHTML=“你赢了!”;
}
}否则{
错误答案++;
guesses.innerHTML=`错误的猜测${错误的答案}`
新问题();
如果(错误答案===3){
gameTitle.innerHTML=“游戏结束!”;
}
}
}
正文{
字体系列:Helvetica、Arial、无衬线字体;
字体大小:15px;
背景:rgb(51,45,45);
身高:100%;
保证金:0;
填充:0;
}
p{
填充:0 2.5雷姆2.5雷姆;
保证金:0;
}
h3、.分数、.错误答案{
文本对齐:居中;
}
html{
字体系列:“Dosis”,无衬线;
身高:100%;
}
#mapDiv{
身高:50%;
}
#主容器{
显示:无;
浮动:对;
宽度:502px;
保证金:30像素自动;
填充:0;
}
#旗筒{
浮动:左;
高度:252px;
宽度:502px;
背景色:rgb(19,16,16);
边框:10px实心rgb(32,13,28);
盒影:2px45px25pxRGB(27,4,4);
}
#旗舰集装箱{
宽度:100%;
身高:100%;
}
#测验容器{
颜色:白色;
背景:rgb(51,45,
function generateAnswers(answer, allCountries) {
    let wrongCountries = allCountries.filter(country => country.alpha3Code !== answer.alpha3Code); 

    const countOfAnswers = 5;
    const positionOfCorrectAnswer = Math.floor(Math.random() * (countOfAnswers + 1));
    const answers = []; 
    for(let i=0; i< countOfAnswers; i++) {
       if (i === positionOfCorrectAnswer) {
          answers.push(answer);
       } else {
         let randomAnswer = wrongCountries[Math.floor(Math.random() * wrongCountries.length)]; 
          wrongCountries = wrongCountries.filter(country => country.alpha3Code !== randomAnswer.alpha3Code); 
          answers.push({ name: randomAnswer.name, alpha3Code: randomAnswer.alpha3Code } );
       } 
    }
    return answers;
};