Unexpected ( on a empty line [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Problem
The following code compiles with following error:
Unexpected token ) path/file.js:3:296
Code
The following script represents a simple function taking two int arguments, and converting them in some way. I won't go into details as the problem is a syntax error.
PS. I am aware this algorithm doesn't work yet, the problem is pure the error chrome is showing up.
function freeTimeCalculator (fuelPrimary, fuelSecondary) {
var freeTime, secondaryFuel, freeTimeFactor = 0.5, fuelSecondaryFactor = 0.3;
function displayResults() {
// Calculate variables
calculateTime();
// Display results
console.log("Free time: " + freeTime);
return console.log("Secondary fuel: " + secondaryFuel);
}
function calculateTime () {
var hours = [], minutes = [];
// Notice: both arrays and indexes getting tied up
hours[0] = minutes[0] = fuelPrimary.toString();
hours[1] = minutes[1] = fuelSecondary.toString();
// Calculation on strings
// Notice: we take advantage that a notation may contain
// many hours even 100, but the minutes will be always
// the last two numbers.
hours[0] = parseInt(hours[0].substr(0, hours[0].length-2));
minutes[0] = parseInt(hours[0].substr(hours[0].length-2, hours[0].length));
hours[1] = parseInt(hours[1].substr(0, hours[1].length-2));
minutes[1] = parseInt(hours[1].substr(hours[1].length-2, hours[1].length));
// Assigning values to the global variables
freeTime = ((hours[0] * 60 + minutes[0]) * freeTimeFactor);
return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);
}
}
}

Change the last line:
return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);
to
return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor;
// ^
EDIT:-
Also the code has extra } brackets. Do remove them. To be precise one extra } bracket at the end

You have a stray ) on the last line. This -
return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);
should be either this -
return secondaryFuel = (hours[1] * 60 + minutes[1]) * fuelSecondaryFactor;
or this -
return secondaryFuel = ((hours[1] * 60 + minutes[1]) * fuelSecondaryFactor);

You have extra ) here
return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);
^

Related

So that the result of the check is always true [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
You need to always be true in the console)
Without gaps, without rounding to the integer, without changing the comparison operator and the general calculation logic, as well as the loop
condition )
P.S. This is an interview task for junior level ...
for (let i = 0; i <= 10; i++) {
console.log(i + ':' + ((i * 0.1) == (i / 10)));
}
// needed to always true in console.log
I would just create a function to compare the difference to a threshold e.g. 0.001.
/**
* Determines if two floating value are equal within a threshold
* #param {Number} a - First floating value
* #param {Number} b - Second floating value
* #param {Number} [t=0.001] - Threshold of difference
* #return {boolean} Whether the difference is less than the threshold
*/
const equalsFloat = (a, b, t = 0.001) => Math.abs(a - b) < t;
for (let i = 0; i <= 10; i++) {
console.log(`${i}: ${equalsFloat(i * 0.1, i / 10)}`);
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

how to get random number between 0 and 1 [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 tried a few ways to get a random number from 1 to 10 and all return undefined or NaN why ?
here is what i tried
var num = Math.floor(Math.random * 10)
function getNum() {
return Math.floor(Math.random * 10);
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);
both dosn't give a number when logged
You need to actually invoke Math.random if you intend for it to generate the random number (ie Math.random())
var num = Math.floor(Math.random() * 10)
function getNum() {
return Math.floor(Math.random() * 10);
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);
Math.random is a method, not a property, so you need to call it as Math.random(). You are also missing a semicolon after your first line.
As stated by #kloddant Math.random is method so you forgot the parentheses ().
So here's the snippet of how you can implement it
var num = Math.floor(Math.random() * Math.floor(10))
function getNum() {
return Math.floor(Math.random() * Math.floor(10));
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);

Are parentheses ignored when assigning JSX to a variable

I'm learning about the javascript basics. The two lines of code seem equivalent and I'm wondering if the parser simply ignores the parentheses in the second case:
var a = 5 + 5;
var b = (5 + 5);
EDIT:
I realize the context of the question might of help after going through the comments, which are helpful. I was trying to assign some JSX to a variable and noticed that the parentheses seem optional:
var a =
<div>
<SomeComponent />
</div>;
var b = (
<div>
<SomeComponent />
</div>
);
In the case of a return statement, I understand that parentheses are needed due to automatic semicolon injection. But in case of a simple assignment do the parentheses still matter?
EDIT 2:
Thanks for the comments and answer that help me narrow down and articulate my question. I think what I really meant to ask is, does JSX parser (e.g. Babel) ignore the parentheses in such a case? Since both will be transpiled into:
var a = React.createElement(
"div",
null,
React.createElement(
SomeComponent,
null
)
)
They're not ignored, but simple mathematics tells you that:
5 + 5 == (5 + 5)
If you had a different set of values, it would change - e.g.:
var a = (5 * 5) + 5 * 5;
var b = 5 * (5 + 5) * 5;
var c = 5 * 5 + (5 * 5);
var d = (5 * 5 + 5) * 5;
var e = 5 * (5 + 5 * 5);
var f = (5 * 5 + 5 * 5);
console.log(a); // 50
console.log(b); // 250
console.log(c); // 50
console.log(d); // 150
console.log(e); // 150
console.log(f); // 50
It depends on the Order of Operations (aka BODMAS/BIDMAS/BEDMAS etc.)

How can I solve this javascript math problem? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I need help with this logic problem in Uri Online Judge site.
The submitted code starts in "var a" and ends in the last console.log, I used the var lines to work with an example of input
var lines = ["3.0", "4.0", "5.2"];
/**
* Code your solution here
*/
var a = parseFloat(lines[0]);
var b = parseFloat(lines[1]);
var c = parseFloat(lines[2]);
var areatr = (a * c) / 2;
var areac = 3.14159 * (c * c);
var areat = ((a + b) * c) / 2;
var areaq = b * b;
var arear = a * b;
console.log("TRIANGULO: " + areatr.toFixed(3));
console.log("CIRCULO: " + areac.toFixed(3));
console.log("TRAPEZIO: " + areat.toFixed(3));
console.log("QUADRADO: " + areaq.toFixed(3));
console.log("RETANGULO: " + arear.toFixed(3));
when I submit my code, the console shows: Wrong answer (30%)
change how you process the input
from:
var lines = input.split('\n');
to:
var lines = input.split(' ');

Unexpected Identifier

I am not sure where this is occurring. I have tried changing around the code to see if the error would go away but I received other errors.
function doesItHit(){
if(toHit * Math.floor(Math.random() * 101) + 1 >= enemyEvasion){
itHits = true
}
}
Don't write set to asign a value to itHits.
Remove this and the eunexpected identifier error will be resolved.
Don't forget to declare the variables needed
var itHits = false;
var enemyEvasion = 100;
function doesItHit(){
if(toHit * Math.floor(Math.random() * 101) + 1 >= enemyEvasion){itHits = true}
console.log("itHits: " + itHits);
}
Here is a fiddle to show, that it works.

Categories