Array not showing up on console [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 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?

Related

Number prediction from 1 to 10 from javascript [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 1 year ago.
Improve this question
This is my code but when i run this code in console always giving me Congratulations.
Help me for solve this problem.
var val=Math.floor(Math.random() * 10) + 1;
console.log(val);
var Predict = Number(prompt("Prediction ?"));
for(var i=1 ; i <= 3; i++){
if(Predict<val){console.log("Up")};
if(Predict=val){console.log("Congratulations") };
if(Predict>val){console.log("Down")}
}
Equal operator assigns the right hand to the left hand and so the result is always true! To compare two values use double equals like this:
if (Predict==val){console.log("Congratulations") };

Javascript compiler exception: missing ) after for-loop control [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
I am getting an error "Javascript compiler exception: missing ) after for-loop control (null.null.script; line 29)" which would be the instantiation of the loop. My syntax looks correct.
a little help please...
var ciArray = [];
var i;
var check;
for (i = 0; check = false; i < ciArray.length; i++) {
if(ciValue == ciArray[i,0]){
ciArray[i,1] += timer;
check =True;
}
if(i == mciArray.length-1 && !check) {
ciArray[i+1,0] = ciValue;
ciArray[i+1,1] = timer;
}
}
You have too many arguments for your for loop - looks more like a while loop with a check clause right now.
inside of condition the loop for you have (i = 0; check = false; <--- in this options it your error.

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()

Error in switch() case; [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 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!

Categories