i guess this is a really simple problem, but i just can't get it to work! I want that my menu should show up again after that i have run a method. Thanks in advance.
function menu () {
var choice = prompt("0. Exit \n\n1. Fahrenheit to Celsius \n2. Celsius to Fahrenheit \n3. Guess a number");
choice = parseInt(choice);
if (choice > 4 || choice < 0) {
alert("FEL!!");
} else if (isNaN(choice)) {
alert("Måste vara en siffra");
}
switch (choice) {
case 0:
choice = false;
break;
case 1:
CelsiusToFarenheit();
break;
case 2:
FahrenheitToCelsius();
break;
case 3:
Guess();
break;
}
return choice;
}
do {
menu();
} while(choice == true);
you forgot to store the variable returned by the menu() function :
function menu () {
var choice = prompt("0. Exit \n\n1. Fahrenheit to Celsius \n2. Celsius to Fahrenheit \n3. Guess a number");
choice = parseInt(choice);
if (choice > 4 || choice < 0) {
alert("FEL!!");
} else if (isNaN(choice)) {
alert("Måste vara en siffra");
}
switch (choice) {
case 0:
choice = false;
break;
case 1:
CelsiusToFarenheit();
break;
case 2:
FahrenheitToCelsius();
break;
case 3:
Guess();
break;
}
return choice;
}
var choice;
do {
choice = menu();
} while(choice == true);
You are calling Menu() and after it executes, it is done. Therefore you need to run Menu() again.
Related
I have around 5 javascript files in my WordPress website, And I tried to execute them one by one based on a boolean condition.
The javascript calls tag1.js if the count is 0.
tag2.js if the count is 1 and so on.
If the count reaches 4, the count resets to 0. And thus this is repeated continously. I tried this code. But it doesn't work even for the first time.
jQuery(document).ready(function( $ ) {
var canClick = true;
var count= 0;
$("body").click(function () {
if (canClick) {
if(parseInt(count) === 0) {
$.getScript("https:example.com/custom_js/atag1.js");
}
else if(parseInt(count) === 1) {
$.getScript("https:example.com/custom_js/atag2.js");
}
else if(parseInt(count) === 2) {
$.getScript("https:example.com/custom_js/atag3.js");
}
else if(parseInt(count) === 3) {
$.getScript("https:example.com/custom_js/atag4.js");
}
else if(parseInt(count) === 4){
$.getScript("https:example.com/custom_js/atag5.js");
count=0;
}
canClick = false;
setTimeout(() => {
canClick = true
},10000);
count = parseInt(count)+1;
}
});
});
Basically this code executes each javascript if the user clicks on the page with timeout differentiating the clicks.
If you want to load the script as per the number of clicks. Then you can look at this solution which will look more precise.
var clicks = 0;
document.addEventListener('click', () => {
loadJS();
++clicks;
});
function loadJS() {
if (clicks === 4) {
clicks = 0;
}
switch (clicks) {
case 0:
console.log('load first js');
break;
case 1:
console.log('load second js');
break;
case 2:
console.log('load 3rd js');
break;
case 3:
console.log('load 4th js');
break;
case 4:
console.log('load 5th js');
break;
default:
// code block
}
}
working demo
How I get out from the loop if switch-case implemented (there is a switch inside the loop).
function playInbestPlace() {
console.log("hello from playInbestPlace ")
findEmptyarea();
for (var i = 0; i < indexOfEmpty.length; i++) {
var elem = indexOfEmpty[i];
switch (elem) {
case 0:
cells[elem].childNodes[0].append("o");
break;
case 2:
cells[elem].childNodes[0].append("o");
break;
case 4:
cells[elem].childNodes[0].append("o");
break;
case 6:
cells[elem].childNodes[0].append("o");
break;
case 8:
cells[elem].childNodes[0].append("o");
break;
}
}
}
I want it to get out if any case valid.
you can add a variable found and break out of the loop if it's true :
function playInbestPlace() {
console.log("hello from playInbestPlace ")
findEmptyarea();
for (var i = 0; i < indexOfEmpty.length; i++) {
var elem = indexOfEmpty[i];
var found = false; // initial found is false
switch (elem) {
case 0:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 2:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 4:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 6:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 8:
cells[elem].childNodes[0].append("o");
found = true;
break;
}
if(found) // break out if it's true
break;
}
}
You could use a flag variable to break from the loop when some condition is verified.
function playInbestPlace() {
console.log("hello from playInbestPlace ");
findEmptyarea();
var keepOnLooping = true;
for (var i = 0; keepOnLooping && i < indexOfEmpty.length; i++) {
if (elem % 2 === 0) {
cells[elem].childNodes[0].append("o");
keepOnLooping = false;
}
}
}
I've also added epascarello optimization in my answer.
I want to simplify this long jquery/javascript code, can you help me? I still learn :)
Here's my jquery code:
$('.pagination-link').click(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
$('.overlay-menu > ul > li > a').click(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
$(window).mousewheel(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
I don't know if I have to use php to get the end of currentAnchor and put it as a parameters. Thank you for your help !
EDIT: I found that the default case is not necessary in my code. But I learn something new if I had my defaut case was important. So, here it's the new one:
function rondClass() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
var currentClass = currentAnchor.replace('active-slide-', 'rond');
$('#rond').removeClass().addClass(currentClass).animate();
}, 50);
}
$('.pagination-link').click(rondClass);
$('.overlay-menu > ul > li > a').click(rondClass);
$(window).mousewheel(rondClass);
Thank you everyone !
You could just replace active-slide- with rond.
var currentAnchor = $('body').attr('class');
var newClass = currentAnchor.replace('active-slide-', 'rond');
$('#rond').removeClass().addClass(newClass).animate();
To handle the default case, you can handle this using indexOf or match:
// indexOf version
if (currentAnchor.indexOf('active-slide-') !== 0) {
$('#rond').removeClass();
} else {
$('#rond').removeClass().addClass(newClass).animate();
}
// match version
if (!currentAnchor.match(/^active-slide-/)) {
$('#rond').removeClass();
} else {
$('#rond').removeClass().addClass(newClass).animate();
}
Replace switch/case with programmatic approach and extract function to avoid code duplication.
var onClick = function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
var slide = currentAnchor.match(/active\-slide\-(\d)/);
if (slide) {
$('#rond').removeClass().addClass('rond' + slide[1]).animate();
} else {
$('#rond').removeClass();
}
}, 50)
})
$('.overlay-menu > ul > li > a').click(onClick);
$('.pagination-link').click(onClick);
A little while ago I needed help with a complicated switch, but now I am substituting the direct variables for arrays. I thought it best to use a for statement, and it isn't displaying the results.
This is a side project for me, so please let me know if there are any aspects you see that could be improved.
<script>
var strMod=0;
var dexMod=0;
var conMod=0;
var intMod=0;
var wisMod=0;
var chaMod=0;
var strength = prompt("what is your strength?");
var dexterity = prompt("what is your dexterity?");
var constitution = prompt("what is your constitution?");
var intelligence = prompt("what is your intelligence?");
var wisdom = prompt("what is your wisdom?");
var charisma = prompt("what is your charisma?");
var abilities=[strength,dexterity,constitution,intelligence,wisdom,charisma];
var abiMod=[strMod,dexMod,conMod,intMod,wisMod,chaMod];
for (var i=0; i<abilities.length;i++){
switch(true){
case (abilities(i)>=0 && abilities(i)<2 && abilities(i)!==null):
abiMod(i)=-5;
break;
case (abilities(i)>=2 && abilities(i)<4):
abiMod(i)=-4;
break;
case (abilities(i)>=4 && abilities(i)<6):
abiMod(i)=-3;
break;
case (abilities(i)>=6 && abilities(i)<8):
abiMod(i)=-2;
break;
case (abilities(i)>=8 && abilities(i)<10):
abiMod(i)=-1;
break;
case (abilities(i)>=10 && abilities(i)<12):
abiMod(i)=0;
break;
case (abilities(i)>=12 && abilities(i)<14):
abiMod(i)=1;
break;
case (abilities(i)>=14 && abilities(i)<16):
abiMod(i)=2;
break;
case (abilities(i)>=16 && abilities(i)<18):
abiMod(i)=3;
break;
case (abilities(i)>=18 && abilities(i)<20):
abiMod(i)=4;
break;
case (abilities(i)>=20 && abilities(i)<22):
abiMod(i)=5;
break;
default:
abiMod(i)= prompt("what is your"+ abilities(i) +"modifier?");
break;
};
alert(abiMod(i));
};
</script>
Rather than using the switch statement, there is a common theme across all case and so a mathematical formula can be used instead.
Here is the revised code
for (var i = 0; i < abilities.length; i++) {
if (abilities[i] !== null && abilities[i] >= 0 && abilities[i] < 22) {
abiMod[i] = Math.floor(abilities[i] / 2) - 5;
}
else {
abiMod[i] = prompt("what is your "+ abilities[i] +" modifier?");
}
};
For starters, you need to access array indices via brackets, not parens.
for (var i=0; i<abilities.length;i++){
switch(true){
case (abilities[i]>=0 && abilities[i]<2 && abilities[i]!==null):
abiMod[i]=-5;
break;
case (abilities[i]>=2 && abilities[i]<4):
abiMod[i]=-4;
break;
case (abilities[i]>=4 && abilities[i]<6):
abiMod[i]=-3;
break;
case (abilities[i]>=6 && abilities[i]<8):
abiMod[i]=-2;
break;
case (abilities[i]>=8 && abilities[i]<10):
abiMod[i]=-1;
break;
case (abilities[i]>=10 && abilities[i]<12):
abiMod[i]=0;
break;
case (abilities[i]>=12 && abilities[i]<14):
abiMod[i]=1;
break;
case (abilities[i]>=14 && abilities[i]<16):
abiMod[i]=2;
break;
case (abilities[i]>=16 && abilities[i]<18):
abiMod[i]=3;
break;
case (abilities[i]>=18 && abilities[i]<20):
abiMod[i]=4;
break;
case (abilities[i]>=20 && abilities[i]<22):
abiMod[i]=5;
break;
default:
abiMod[i]= prompt("what is your"+ abilities[i] +"modifier?");
break;
};
alert(abiMod[i]);
};
</script>
Alright, I have made some JavaScript for an assessment. Everything worked fine until I put in a new function with a switch statement called:
function differentComments(answer) {
The program doesn't seem to load the follow function anymore when the function differentComments is in there:
function genQuestion() {
All of my JavaScript code is below (HTML is available on Pastebin):
var x, y; //the global variables
function aNumber() {
return Math.floor(1 + Math.random() * 12);
}
function genQuestion() {
x = aNumber();
y = aNumber();
dout = document.getElementById('Question');
dout.value = x + " times " + y;
}
function buttonPressed() {
var input = document.getElementById('Typed').value;
var answer = x * y;
if (input == answer) {
differentComments("Right");
genQuestion();
} else {
differentComments("Wrong");
}
document.getElementById('Typed').value="";
}
function differentComments(answer) {
var random_number = Math.floor(1 + Math.random() * 4);
if (answer == "Right") {
switch (random_number) {
case 1:
window.alert("Very Good!");
break;
case 2:
window.alert("Excellent!");
break;
case 3:
window.alert("Correct - Nice work!");
break;
case 4:
window.alert("Correct - keep up the good work!");
break;
default:
break;
}
} else (answer == "Wrong") {
switch (random_number) {
case 1:
window.alert("No. Please try again.");
break;
case 2:
window.alert("Wrong. Try once more.");
break;
case 3:
window.alert("Incorrect – Don’t give up.");
break;
case 4:
window.alert("No – keep trying.");
break;
default:
break;
}
}
}
Your else clause is incorrect.
if (answer == "Right") {
switch(random_number) {
...
}
else (answer == "Wrong") {
}
won't parse because the second test is missing an if.
if (answer == "Right") {
switch(random_number) {
...
}
else **if** (answer == "Wrong") {
}
Essentially you're treating an if() as if it's a switch(), and that's not syntactically correct.
The first thing you should do when you run into situations like this is use either jshint or jslint to check the syntactical correctness of your code.
In your code block :
if (answer == "Right") {
switch(random_number) {
...
}
else (answer == "Wrong") {
switch(random_number) {
...
}
}
mean you put if and else statement and a else condition only execute when all if conditions are return false. So there is no need to put conditional-statement(like in your case -else (answer == "Wrong") {) ).
You can simply write this :
if (answer == "Right") {
switch(random_number) {
...
}
else {
switch(random_number) {
...
}
}
It's means if your answer is not equal to Right, it always go to else statement.
OR
If you want to check for more conditions, use else if(){} statements.
if (answer == "Right") {
switch(random_number) {
...
}
else if (answer == "Wrong") {
switch(random_number) {
...
}
}
Read this