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.
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 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 4 years ago.
Improve this question
I'm trying to add spaces to my input value while the user typing, it works for the constant digits like after 4 letters or after 5 letters. But I couldn't manage to add space after certain digits dynamically.
for example
if user types 1234567890
blocks: [3,3,4]
expected output: 123 456 7890
if user types 1234567890
blocks: [3,2,2,3]
expected output is: 123 45 67 890
Handling inputs as the user types can be tricky.
Although not perfect, one solution is to calculate what the expected result should be, and then compare with what it currently is. If they are different then update the actual input with the calculated one.
Below is a simple example,.. It will even handle were the user pastes in the numbers.
One issue with the below is the cursor position if say you inserted the number mid way, but this could be maybe handled with remembering the cursor position and restoring.
const splits = [3,3,4];
$('#a').on("input", function(){
let count = 0;
const breaks = splits.map(m => { const ret = m + count; count += m; return ret; });
const a = this.value.split("").filter(f => f !== ' ');
const s = a.map((m, ix) => {
if (breaks.includes(ix + 1)) return m + " "
else return m;
}).join("");
if (this.value.trim() !== s.trim()) this.value = s;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="a"/>
Try the below code
function splitValue(value, index) {
return value.substring(0, index) + " " + value.substring(index);
}
var test=[3,3,4];
var secondVar = '';
var firstVar = '1234567890';
for(let i = 0; i < test.length; i++){
var TestVar = splitValue(firstVar, test[i]);
secondVar = secondVar + " " + TestVar.split(" ")[0];
secondVar=secondVar.trim();
if(i == 0)
firstVar = firstVar.replace(secondVar.trim(),'');
else
firstVar = firstVar.replace(secondVar.split(' ')[i + 1],'');
}
console.log(secondVar);
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');