Error in switch() case; [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
While I'm trying to write a simple function which need to iterate through classes of event.target and do something depending of class name, I've encountered unknown error (as for me).
function tPager() {
var lc = $(event.target).attr('class');
swith(lc) {
case ('slow'):
console.log('slowclicked');
break;
case ('page'):
console.log('pageclicked');
break;
}
};
This is just for testing purposes, console will always say "Unexpected token", there is an error in line 3, "{". Can't get what's wrong.
JSFiddle: http://jsfiddle.net/2cJhC/

Wouldn't the code be like this
switch(lc) { // note the keyword switch
case 'slow':
console.log('slowclicked');
break;
case 'page':
console.log('pageclicked');
break;
}
You're currently having this
swith(lc) { // that is not switch keyword.
The mistake actual, was at the keyword usage. You were having the wrong keyword that was triggering the mistake. Change it, and its good to go!

Related

node JS more than is incorrect [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi im pretty sure what im about to post might not be enough info (if so please let me know what more is needed). I am using node js and having a really weird error. Below is the code and output.
if (currentPrice > variableData[i].stopLossHard) {
console.log('if')
console.log(currentPrice)
console.log('is more than')
console.log(variableData[i].stoplossHard)
}
Output:
if
92.7
is more than
93.62700000000001
This is consistently happening. I also made sure that both currentPrice and variableData[i].stopLossHard are numbers and not strings (I made sure in the code and in the output its the color of a number not a string)
Any ideas is highly appreciated.
The attribute you print is different than the one you check in the if statement:
(In the if stopLossHard has a capital L, stop-L-ossHard, what you print doesn't)
Try this:
if (currentPrice > variableData[i].stoplossHard) {
console.log('if')
console.log(currentPrice)
console.log('is more than')
console.log(variableData[i].stoplossHard)
}

Array not showing up on console [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have no clue why this code doesn't work.
var tipCalculator = [1.2,1.15,1.10];
var restaurantBills = [124,48,738,10,300,150];
var finalBills = [];
function calculateBills(){
for (var i = 0 ; i < restaurantBills.length ; i++){
switch(true){
case restaurantBills[i]<50:
finalBills.push(restaurantBills[i]*tipCalculator[0]);
break;
case restaurantBills[i]>50 && restaurantBills[i]<200 :
finalBills.push(restaurantBills[i]*tipCalculator[1]);
break;
case restaurantBills[i]>200:
finalBills.push(restaurantBills[i]*tipCalculator[2]);
break;
default:
break;
};
};
return finalBills;};
console.log(calculateBills);
calculate bills is a function and so must be called
calculateBills();
then your code should be fine
As pointed by jonrsharpe you never called the function that is returning the array.
To make a function execute you need to call it.
change the line from
console.log(calculateBills);
to
console.log(calculateBills());
for debugging
Browsers like Chrome and Firefox comes with javascript debugger. Goto dev tools -> debugger where you would be able to set break points and trace the execution of your code.
Get Started with Debugging JavaScript in Chrome DevTools
How can I debug my JavaScript code?

If/else statements not displaying correctly to console [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to get the console to read "No goal!" but every time I run it, I get "Goal". I'm guessing there's some basic syntax error that I'm making?
let isSoccerFan = false;
if (isSoccerFan=true) {
console.log("Goal!");
} else{
console.log("No goal!")
}
Your code is not correct. Try this:
let isSoccerFan = false;
if (isSoccerFan) {
console.log('Goal!');
} else {
console.log('No goal!');
}

javascript for loop does not work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm creating a site and i needed to do some js. I'm not that good with it but tought i would figer it out. Not. I created a for loop but it does not run.
function order(user,product){
var index;
for(var i = 0; i<users.lenght; i++){
if(user == users[i]){
index = i;
break;
}
}
var budget = budgets[index];
alert(budget);
}
the creation of the users and budgets arrays are done with php and after checking with alert() it was how it should be.
Can anyone help me please?
lenght is spelt length. The misspelt property does not exist, so it undefined, which is equivalent to 0.

JavaScript giving nonsense errors [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So, me and a few friends are making a text adventure in JavaScript. in it, there is a possibility of the player using a heal spell, which runs the following code:
function heal() {
alert('You chant some ominous-sounding words, and your wounds heal');
alert('Hit points restored!');
hitPoints += 60;
blobsOfDoom -= 30;
burn();
MonsAtt();
Choose Spell();
}
Error Messages:
Firefox:
/*
Exception: SyntaxError: missing ; before statement
#Scratchpad/1:2703
*/
Chrome:
SyntaxError: Unexpected identifier.
Why is this error showing up? How do I fix it?
You cant have spaces in functions
Choose Spell();
Check your function declaration for ChooseSpell and any other occurances, and change them to a valid function name, like chooseSpell()

Categories