Javascript not working as I want to - javascript

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

Related

Condensing several if statements into a for and an if

Is there a way to condense these four if statements into a for loop and with a single if condition? I was told that there is a way but I can only think of multiple else if statements.
function New() {
if (foodInOven == true) {
timeOfCooking += 1;
}
if (timeOfCooking == 10) {
console.log("cooked pasta");
} else if (timeOfCooking == 15) {
console.log("burning pasta!");
} else if (timeOfCooking == 20) {
console.log("pasta burnt!!");
}
}
One option could be to have a lookup for your times and, if there's a value, log it to the console.
const cookingLookup = {
10: "cooked pasta",
15: "burning pasta!",
20: "pasta burnt!!"
}
function New() {
if (foodInOven == true) {
timeOfCooking += 1;
}
if (cookingLookup[timeOfCooking]) {
console.log(cookingLookup[timeOfCooking]);
}
}

if statement compare places

We are working on another game and we have several answers to a question. Now the problem is that I need to compare the place of the whale to the place of a fish with the answer.
Does anyone know how to do that?
var Keys = {
left: 37,
right: 39,
up:38,
down:40
}
function Start(){
$("#start").hide();
alert ("test start weg");
Opdr1();
}
$(function(){
$(document).keydown(function(e){
switch(e.which){
case Keys.right:
$("#whale").css("left", "+=10px");
$("#whale").attr("src", "pacman-right.png");
break;
case Keys.left:
$("#whale").css("left", "-=10px");
$("#whale").attr("src", "pacman-left.png");
break;
case Keys.up:
$("#whale").css("top", "-=10px");
break;
case Keys.down:
$("#whale").css("top", "+=10px");
break;
}
});
});
function Opdr1(){
$(".bubbel").css("visibility", "visible");
$(".vis1-1").css("visibility", "visible");
$(".vis2-1").css("visibility", "visible");
$(".vis3-1").css("visibility", "visible");
$(".vis4-1").css("visibility", "visible");
//if the whale's css left and top is the same as the left and top of the vis-1 then { }
if($("#whale").css("left") == ("left") {
alert('vis-1 fout');
}
if($("#whale").css('left') == (".vis2-1").css('left')) {
alert('vis-2 fout');
}
if($("#whale").css('left') == (".vis3-1").css('left')) {
alert('vis-3 fout');
}
if($("#whale").css('left') == (".vis4-1").css('left')) {
alert('vis-4 fout');
}
}
You need to compare the offset of the two objects
For example,
if ( $("#whale").offset().top == $(".vis-1").offset().top )
{
}
similarly
if ( $("#whale").offset().left == $(".vis-1").offset().left )
{
}

nested if/else statements talked about assigning variables...I've already done that and it still doesn't work

Why won't my nested if/else statements work: it takes me to bing.com as a search engine no matter what adult age I enter; everything else works correctly.
function adult_jump(SelectSE, SelectWD, age) {
if (SelectSE==true) {
if (age <= 45) {
window.location = "http://www.google.com"
} else {
window.location = "http://www.bing.com"
}
} else {
if (SelectWD==true) {
window.location = "http://www.yahoo.com"
} else {
window.alert("You must select a search tool!")
}
}
}
while comparing with true or false.. it is better to use the === operator.. i modified the window.location to alert.. so you can see all results at once here
function adult_jump(SelectSE, SelectWD, age) {
if (SelectSE === true) {
if (age <= 45) {
alert("http://www.google.com") ; }
else { alert("http://www.bing.com") ; }
}
else {
if (SelectWD === true) {
alert("http://www.yahoo.com") ; }
else { alert("You must select a search tool!") ;}
}
}
adult_jump(true,false,30); // alerts google
adult_jump(true,false,48); // alerts bing
adult_jump(false,true,0) ; //alerts yahoo
adult_jump("","",0) ; // alerts select a search tool

Loop program again after method is executed

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.

"else if" statement within for loop within larger if/else if/else statement

Is it possible to do something like this in JavaScript?
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
for (i in exampleObject) {
else if (name == exampleObject[i].name) {
exampleFunction(exampleObject[i].arg);
}
}
else {
exampleFunction('default')
}
I tried it, but got an "unexpected keyword else on line 8" (the "else if" within the for loop). Is there another way to do this?
edit: updated this to use exampleObject[i] in the loop. My bad!
No. I think the best way to accomplish this is to move the for loop into an else block and do the following
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
var isFound = false;
for (i in exampleObject) {
if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
isFound = true;
}
}
if (!isFound) {
exampleFunction('default')
}
}
Note: It looks like there are other errors in this code. The for loop declares the i iteration variable but never actually uses it. Did you mean for the if check in the for loop to use i instead of name?
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
var isFound = false;
for (i in exampleObject) {
if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
isFound = true;
break;
}
}
if (!isFound) {
exampleFunction('default')
}
}
Here is the correct solution. It short circuts the if statements in the loop just like else if would short circuit. This is the same solution as #1 but it correctly short circuits.
The following code looks wrong to me , have the for loop inside if block
for (i in exampleObject) {
else if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
}
that is not possible. I would try an come up with a better example to show you how to do what you want, but honestly I am not sure what you want to do. The for loop is confusing me. Can you provide some more information?
In a word, no. You are terminating the if-statement block with the last brace before the for statement.
Well for one, shouldn't this:
for (i in exampleObject) {
else if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
}
}
be this:
for (i in exampleObject) {
else if (name == i.name) {
exampleFunction(i.arg);
}
}
Though i don't know much (if anything) about js, this is just a guess at something that isn't even the problem you're talking about.
Would you be adverse to doing it like this:
bit = 0;
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
bit = 1;
}
bit2 = 0;
while(bit == 1){
for (i in exampleObject) {
if (name == i.name) {
exampleFunction(i.arg);
bit = 0
bit2 = 1;
}
}
}
if(bit2 = 0){
exampleFunction('default');
}
?
Something like this may help?
found = false
if (name == 'foo') {
found = true
exampleFunction('some_arg');
}
else if (name == 'bar') {
found = true
exampleFunction('another_arg');
}
else {
for (i in exampleObject) {
if (name == i.name) {
exampleFunction(i.arg);
found = true
break;
}
}
}
if !found:
exampleFunction('default')

Categories