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);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'am newbie in Python and i'am having a hard time trying to translate this Javascript arrow function into Python. I'am not able to make the part where i use substring in JS to get the next 3 values in my loop when i find '\x1D'. Any tips or suggestions ?
module.exports = edi => {
let decompressedEdi = ''
let lastCompressor = 0
for (let i = 0; i <= edi.length; i++) {
if (edi[i] === '\x1D') {
let decimal = parseInt(edi.substring(i + 1, i + 3), 16)
let repeater = edi[i + 3]
decompressedEdi +=
edi.substring(lastCompressor, i) + repeater.repeat(decimal)
lastCompressor = i + 4
}
}
decompressedEdi += edi.substring(lastCompressor, edi.length)
return decompressedEdi.replace(/(\r\n|\n|\r)/gm, '')
}
In python, strings can be sliced like arrays :
for i, c in enumerate(edi):
if c == '\x1D':
decimal = int(edi[i+1:i+3], 16)
The int function has the following signature: int(str, base)
from re import sub
def decompress(edi):
decompressed = ""
last_compressor = 0
for i, c in enumerate(edi):
if c == "\x1D":
repetitions = int(edi[i + 1: i + 3], 16)
repeating_char = edi[i + 3]
decompressed += edi[last_compressor:i] + repeating_char * repetitions
last_compressor = i + 4
decompressed += edi[last_compressor:-1]
return sub("\r\n|\n|\r", decompressed)
How I read the code
Feel free to ignore this bit, but it might help.
Given edi which has a len, for each edi that matches \x1D, get the substring of edi from the index + 1 to index + 3 as a hexadecimal integer as set as decimal. The repeater is the index + 3'th element of edi for each element and it is expected to be a str. It will be repeated the hexadecimal number of times defined in decimal, but only after the substring of edi from lastCompressor to the current index. On each iteration where \x1D is matched, the lastCompressor is increased by 4.
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
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>
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.