how can 2 function share common variable in JavaScript - javascript

var ques=["Qno 1", "Qno 2","Qno 3","Qno 4","Qno 5","Qno 6","Qno 7","Qno 8","Qno 9","Qno 10"];
var ans =[1,2,3,4,5,6, 7,8,9,10];
function random()
{
var qno=Math.floor((Math.random() * 10) );
return qno;
}
function validate()//here need help
{
var Rans=document.getElementById("numb").value;
if (Rans == ans[qno])
alert("validated!!!");
else
alert("Not a valid entry.. declined!!!");
}
function execution()
{
var x = document.getElementById("myDIV");
if (x.style.display === "none") { x.style.display = "block";}
else {x.style.display = "none";}
var qno1=random();
document.getElementById("demo").innerHTML = ques[qno1];
var qno2=random();
if(qno1==qno2)
qno2=random();
document.getElementById("demos").innerHTML = ques[qno2];
}
in the above code I am trying to generate random questions. this is only javascript part of my code.
starting with first the 2 arrays are questions and sol respectively. code is executing successfully but I have problem in validation part. for validation i want to pass qno1 value to validation function on pressing of submit button in HTML button tag. please help me with this code to execute and how can we share common variable among 2 functions.

You need to scope the vars to outside of your functions so you can use them in both.
var ques=["Qno 1", "Qno 2","Qno 3","Qno 4","Qno 5","Qno 6","Qno 7","Qno 8","Qno 9","Qno 10"];
var ans =[1,2,3,4,5,6, 7,8,9,10];
//move scope of vars
var qno1
function random()
{
var qno=Math.floor((Math.random() * 10) );
return qno;
}
function validate (qno)// use it here by passing it in
{
var Rans=document.getElementById("numb").value;
if (Rans == ans[qno])
alert("validated!!!");
else
alert("Not a valid entry.. declined!!!");
}
function execution()
{
var x = document.getElementById("myDIV");
if (x.style.display === "none") { x.style.display = "block";}
else {x.style.display = "none";}
// set here
qno1=random();
document.getElementById("demo").innerHTML = ques[qno1];
var qno2=random();
if(qno1==qno2)
qno2=random();
document.getElementById("demos").innerHTML = ques[qno2];
}
// call it wherever
validate(qno1)

Related

JS : press enter and go to the next input

(Sorry if my english is bad)
I try to make a little game where you have to answer question in inputs. When you valid with the key "Enter", next input appear, and a new question in.
It is complicated to explain, so I leave you the test URL : nicolaslorand.com/bac.php
Here is my a part of my code :
var i = 1;
var j = 2;
$('#input'+i).keypress(function(event) {
console.log('input actuel :'+i);
console.log('input suivant :'+j);
if (event.which == 13) {
verification();
console.log("Touche entrée");
}
});
function verification(){
document.getElementById('input'+j).style.display = "block";
var index = $(".inputform").index(this) + 1;
$(".inputform").eq(index).focus();
var recup = document.getElementById('input'+i);
var verif = recup.value.toUpperCase();
var divLettre = document.getElementById('lettre');
var premiereLettre = divLettre.innerText || divLettre.textContent;
if ( verif.charAt( 0 ) === premiereLettre ) {
$("#input"+i).addClass('trueanswer');
i++; j++;
scoreTotal++;
console.log(i);console.log(j);
}
else{
$("#input"+i).addClass('falseanswer');
i++; j++;
console.log(i);console.log(j);
}
With this code, when I press enter, next input appear, but I have to write in the first input so that my answer is verified by the function.
You are using this inside function this refers to window object. i think you should use i instead of this
var index = $(".inputform").index(i) + 1;

Alternative to This in Javascript

I have this code on codepen: https://codepen.io/RaoniSousa/pen/Zpkxbb:
var x = document.querySelectorAll('.label'), //label com ID label,
z = document.querySelectorAll('.input'); //input com ID name;
function myFunction() {
'use strict';
if (this.value !== "" ) {
this.style.opacity = '1';
this.style.bottom = '4em';
this.style.color = '#722872';
console.log(this);
} else {
this.style.opacity = '0';
this.style.bottom = '1.5em';
console.log(this);
}
}
In the function above i'd like to change only the style of the label (var x). I know the 'this' is referring to .input (var z), but, i'd like to apply a style to label as i change his related input value, but if i use a for loop, he calls all label one the same time. Is there a way to call var x intead of var z by using 'this.style' or somebody knows another alternative to this code?
I'd like it works as happens here(roll down the bar till reach Contact Me section): https://codepen.io/freeCodeCamp/pen/YqLyXB
Thanks in advance.
try this
function myFunction() {
'use strict';
if (this.value !== "" ) {
this.nextElementSibling.style.opacity = '1';
this.nextElementSibling.style.bottom = '2em';
this.nextElementSibling.style.color = '#722872';
console.log(this.nextElementSibling);
} else {
this.style.opacity = '.4';
this.style.bottom = '.5em';
console.log(this);
}
}

Automatic moving Input

I'm trying to have the Input someone puts into the Cell "M19" move to "Z1" and if that Cell is full move it to "Z2" and so on so forth.
Currently my code accurately moves the first and second Input into "M19" into "Z1" and "Z2" but afterwards just stops doing anything.
function myFunction()
{
var ss = SpreadsheetApp.getActiveSheet();
var vZPos = "M19";
var zZPos = "Z1";
var vonZelle = ss.getRange(vZPos); //InputCell
var zuZelle = ss.getRange(zZPos); //FirstOutputCell
var i = 0;
var c = 2;
var cTos = c.toString();
var naechsteZuZelle = zZPos.replace("1", cTos);
var naechsteZuZelleRange = ss.getRange(naechsteZuZelle); //ChangingOutputCell
do
{
if (zuZelle.isBlank() == true && c == 2)
{
vonZelle.moveTo(zuZelle);
i++;
}
else
{
if (naechsteZuZelleRange.isBlank() == true)
{
vonZelle.moveTo(naechsteZuZelleRange);
i++;
}
else
{
c++;
}
}
}
while (i == 0);
}
Since I'm not that skilled at coding I've kinda hit a brick wall on how to go on about doing things, I would aprreciate any help and/or explanations on how to solve my Problem.
P.S. Since I'm coding in German some of the Variable names might seem weird, if there are any questions I'll do my best to translate/elaborate on them.
Change:
else
{
c++;
}
To:
else
{
c++;
cTos = c.toString();
var naechsteZuZelle = zZPos.replace("1", cTos);
var naechsteZuZelleRange = ss.getRange(naechsteZuZelle)
}
It was not in your loop.

I broke a working javascript script trying to expand it

The script below was working properly (toggling display on/off) before I added lines 3 to 8 to avoid displaying more than one element at a time (there are hundreds). It still works after adding lines 3 to 8 but it does not toggle back to "display:none;" (there is always one element visible).
I only have basic knowledge of Javascript and I cannot see what I am doing wrong. Can someone give me a hint/solution?
<script type="text/javascript">
function toggle_visibility(id) {
numb = document.forms.length
for(i=1;i<numb+1;i++) {
j="N"+i
elemnt = document.getElementById(j);
elemnt.style.display = "none";
}
var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}
</script>
You are not declaring i as local variable in for -loop.Instead use this.Also you have not declared elemnt variable.Hope you dont want it to be global variable
for(var i=1;i<numb+1;i++) {
j="N"+i
elemnt = document.getElementById(j);
elemnt.style.display = "none";
}
Try declaring all the variables first:
function toggle_visibility(id) {
var i, elemnt, state,
numb = document.forms.length + 1;
for( i = 1; i < numb; i++ ) {
elemnt = document.getElementById( "N" + i );
elemnt.style.display = "none";
}
elemnt = document.getElementById( id );
state = elemnt.style.display === 'none';
elemnt.style.display = state ? 'block' : 'none';
}

Passing a javascript value

I want to pass a numeric value through to the following Javascript function.
function swap2() {
var oldDiv = document.getElementById("product-grid");
var newDiv = document.getElementById("product-page");
oldDiv.style.display = "none";
newDiv.style.display = "block";
}
I want to be able to call the function with a number in the bracket like...
onclick="swap2(2)"
And then have the newDiv variable change based on that number like so...
var newDiv = document.getElementById("product-page2");
How can I go about doing this?
function(variable){
// process using 'variable'
}
that's how you pass a variable to a function. Thus:
function swap2(n) {
var oldDiv = document.getElementById("product-grid");
var newDiv = document.getElementById("product-page" + n);
oldDiv.style.display = "none";
newDiv.style.display = "block";
}

Categories

Resources