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 6 years ago.
Improve this question
I've got some code like this:
a = "string1" + b || c + "string2";
And it's working as intended except string2 is left off the end when it's run.
Is there a way to make this work with parentheses perhaps? Or maybe another method?
Edit: The idea was for the code to concatenate string1, result of b || c, and string2 together.
+ has higher precedence than ||. What this means is that your code effectively means this:
var temp1 = "string1" + b;
var temp2 = c + "string2";
a = temp1 || temp2;
If you want the string to start with "string1", end with "string2", and have either b or c in the middle, then you can wrap the || section in parentheses to ensure it's evaluated before the concatenation.
a = "string1" + (b || c) + "string2";
Example:
function log(msg) {
document.querySelector('pre').innerText += msg + '\n';
}
var a;
var b = false;
var c = "__C__";
a = "string1" + (b || c) + "string2";
log(a);
b = "__B__";
a = "string1" + (b || c) + "string2";
log(a);
<pre></pre>
Related
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
I'm studying in the 1st year at a university in Italy. We started studying js on different online courses...
I have to do an exercise in a few days, and I know, it's very simple.
I have a function " max (a,b) " and I have to print out which is the biggest number, if A or B.
I created a function, and I don't know how to give them a value in input from my keyboard, and I also don't know how to print out a string like : "the biggest number is (return A) or (b).
function max(a,b) {
a = Number(prompt("INSERISCI IL PRIMO NUMERO: "));
b = Number(prompt("INSERISCI IL SECONDO NUMERO: "));
if (a > b) {
console.log()
}
}
obv it doesn't work because A and B were declared first at the beginning, but I don't know how to give them a value...
thank you guys, I'm waiting for updates.."
You have everything there, you just need to separate it so that the values from a and b are passed to your max function.
function max(a, b) {
if (a > b) {
console.log(a + " > " + b)
}
else if(a == b){
console.log(a + " = " + b)
}
else{
console.log(b + " > " + a)
}
}
a = Number(prompt("INSERISCI IL PRIMO NUMERO: "));
b = Number(prompt("INSERISCI IL SECONDO NUMERO: "));
max(a, b);
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(' ');
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 6 years ago.
Improve this question
So here is the scenario, I have two values:
var a=1
var b=23
I want a value like (a.b) 1.23:
var a=10
var b=45
I want a value like (a.b) 10.45
Is it possible? Then how?
var a=10; var b=45;
var result = +(a + '.' + b);
+ operator is unary operator for converting a string into a number.
See Mozilla Developer Network.
var a = 10;
var b = 45;
var res = a + (b/100);
Combine them into a string, and then run the result through parseFloat:
var a=1
var b=23
var result = parseFloat(a + "." + b);
Edit: As per #Vostrugin's comment, you can ensure the number of decimal places using toFixed()
parseFloat('0.0').toFixed(2) // this will give you 0.00
var a = 1;
var b = 2;
var c = a + "." + b;
alert(c);
The easiest way to do this is through string concatenation.
It would look like +(a + "." + b)
In JavaScript, a string added to a number becomes a string. A number appended to a string, and the string remains.
However, then we need to convert it to a number. We do that by either using parseInt, or +
Here is an example you can try for yourself
var n1 = document.getElementById("n1");
var n2 = document.getElementById("n2");
var result = document.getElementById("result");
function convertNumber() {
result.innerHTML = (+(n1.value + "." + n2.value));
}
#number-converter {
margin-top: 10px;
}
Number 1: <input type="text" id="n1" />
Number 2: <input type="text" id="n2" />
<button id="number-converter" onclick="convertNumber()">Calculate Number</button>
<p id="result"></p>
var test=function(a,b){
return parseFloat(a + "." + (b>10||b===0?b:(b+"5"))).toFixed(2);
};
console.log(test(1,23));
console.log(test(10,4));
console.log(test(0,0));
//That will always work
//THATS IF YOU WANT THE 10.45 ROUND EFFECT THAT YOU QUEST
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 have tried to code this problem by converting the looped variables from 100 to 999 to strings, seperate it's digits to numerical values, find the sum of them when cubed, it seems correct when I calculate it on pen and paper, however when I log it it just prints all looped numbers from 100 to 999 , he is my code
for (var i=100; i<=999; i++){
var x = i.toString();
var z = i
var a = parseInt(x[0]);
var b = parseInt(x[1]);
var c = parseInt(x[2]);
var y = (a * a * a) + (b * b * b) + (c * c * c);
if (y = z){console.log ("a happy number is " + x);}
}
Here is a jsfiddle for you JS FIDDLE LINK
for (var i=100; i<=999; i++){
var myString = i.toString();
var a,b,c;
//this is how I would split string apart
a =myString.substring(0,1);
b =myString.substring(1,2);
c =myString.substring(2,3);
var y = (a * a * a) + (b * b * b) + (c * c * c);
//this was your logic error
if (y === i){console.log ("a happy number is " + i);}
}
console.log('done');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I am trying to create a tool to convert temperature values from Celsius, Fahrenheit, or Kelvin but the console says the function getTemperature() is undefined?
function getTemperature() {
var fahrenheit = document.formBox.tempF.value;
var celsius = document.formBox.tempC.value;
var kelvin = document.formBox.tempK.value;
var select = document.formBox.select.value;
//User gives initial temperature in fahrenheit, convert to C and K
if (select === "GivenF") {
var c = (5/9)*(fahrenheit-32);
var k = ((((fahrenheit-32)*5)/9) + 273.15);
document.getElementById("celsius").innerHTML = "This equals " + c " degrees celsius.";
document.getElementById("kelvin").innerHTML = "This equals " + k " kelvin.";
document.getElementById("fahrenheit").innerHTML = "";
}
//User gives initial temperature in celsius, convert to F and K
else if (select === "GivenC") {
var f = ((9/5)*celsius)+32;
var k = celsius+273.15;
document.getElementById("celsius").innerHTML = "";
document.getElementById("kelvin").innerHTML = "This equals " + k " kelvin.";
document.getElementById("fahrenheit").innerHTML = "This equals " + f " fahrenheit.";
}
//Use gives initial temperature in kelvin, convert to F and C
else if (select === "GivenK") {
var f = (9/5)*(kelvin-273)+32;
var c = kelvin-273.15;
document.getElementById("celsius").innerHTML = "This equals " + c " degrees celsius.";
document.getElementById("kelvin").innerHTML = "";
document.getElementById("fahrenheit").innerHTML = "This equals " + f " fahrenheit.";
};
}
There are syntax errors like
... = "This equals " + c " degrees celsius.";
where you miss + sign after variable.
It may be something with the way you call the function also, but syntax erros are in the first place.