hello i'm very new to javascript so forgive me if the answer seems obvious...
this is my code which is executed at the click of a button in the body
function q() {
var A = document.getElementById("time_one").value;
var B = document.getElementById("time_two").value;
var C = document.getElementById("post_number").value;
var D = (B - A) / C;
for ( var x = A; x < B; x = x + D ) {
document.getElementById("q_box").innerHTML = x + "<br />";
}
}
i'm pretty sure the error is in the for loop... the output is supposed to be a list of numbers between A and B. and changing the innerHTML = to innerHTML += hasn't worked either?
function q() {
var A = +document.getElementById("time_one").value;
var B = +document.getElementById("time_two").value;
var C = +document.getElementById("post_number").value;
var D = (B - A) / C;
for ( var x = A; x < B; x = x + D ) {
document.getElementById("q_box").innerHTML += x + "<br />";
}
}
You should convert the values in int and you should use +=
With innerHTML code inside the for loop you are always setting the value with the last time iterated value. Hence, you need to update your code to
for ( var x = A; x < B; x = x + D ) {
document.getElementById("q_box").innerHTML += x + "<br />";
}
OR
var y = "";
for ( var x = A; x < B; x = x + D ) {
y += x + "<br />";
}
document.getElementById("q_box").innerHTML = y;
I will recommend you to go for 2nd option, as it is better to set the updated value at once and not to extract the value and update for each iteration.
Related
is there a way where x and y, when returned, can retain the value that has been inputted instead of being undefined?
Just in case you need the objective of the code here it is:
Call the function inputNumber to accept 2 values (var x and y turned into int a and b) then call getAverage. Call inputNumber again to input the value for var z turned into int c then call checkInterval.
Here is my code:
var i = 0;
function inputNumber() {
if(i == 1){
var z = prompt("Input the value of c");
var c = parseInt(z);
checkInterval(a,b,c);
}else{
var x = prompt("Input the value of a");
var y = prompt("Input the value of b");
return x;
return y;
}
}
var a = parseInt(x);
var b = parseInt(y);
getAverage(a,b);
function getAverage(a,b) {
average = (a + b)/2;
div1.innerHTML = "The average of a and b is " + average;
i++;
return i;
}
inputNumber();
function checkInterval(a,b,c) {
if(a<b && a<c<<b || b<a && b<c<a){
div2.innerHTML = "c is an interval of a and b";
alert("TRUE");
}else{
div2.innerHTML = "c is not an interval of a and b";
alert("FALSE");
}
}
<body onload="inputNumber()">
<div id="div1"></div>
<div id="div2"></div>
</body>
I have updated your solution with my interpretation.
Some issues that I identified in your code.
A javascript function cannot return more that one value. In that scenario, you have to go for returning a javascript object.
Also there was some issue in your comparison logic in checkInterval function, I have implemented my own logic there.
var i = 0;
const div1 = document.getElementById('div1');
const div2 = document.getElementById('div2');
const { x, y } = inputNumber();
function inputNumber() {
if (i == 1) {
var z = prompt("Input the value of c");
var c = parseInt(z);
checkInterval(a, b, c);
} else {
var x = prompt("Input the value of a");
var y = prompt("Input the value of b");
i++;
return {
x: x,
y: y,
}
}
}
var a = parseInt(x);
var b = parseInt(y);
getAverage(a, b);
function getAverage(a, b) {
average = (a + b) / 2;
div1.innerHTML = "The average of a and b is " + average;
return i;
}
inputNumber();
function checkInterval(a, b, c) {
if ((a < c && c < b) || (b < c && c < a)) {
div2.innerHTML = "c is an interval of a and b";
alert("TRUE");
} else {
div2.innerHTML = "c is not an interval of a and b";
alert("FALSE");
}
}
<div id="div1"></div>
<div id="div2"></div>
Your variables x and y are function scoped so they are available only within the function where they are defined. So, you need to define it in the outer scope so that all functions have access to it. See the code snippet below, it should help!
Also, avoid using innerHTML use innerText or textContent instead.
var x;
function demo() {
x = prompt("Enter a number");
after();
}
function after() {
alert("Your number is " + x);
}
<body onload="demo()">
</body>
Can someone walk me thru this example and why it prints 10? I see to be getting confused on the function in function aspect I think. Also what is the difference between var x = and x = ?
var x = 6;
var y = 4;
var a = function(b) {
return function(c) {
return y + b + c;
}
};
x = 2;
y = 5;
var fn = a(x);
x = 1;
y = 3;
var unknown = 5;
console.log(fn(unknown));
var x = 6;
var y = 4;
var a = function(b) {
return function(c) {
return y + b + c;
}
};
x = 2;
y = 5; // unnecessary, meant to confuse
var fn = a(x); // creates a closure, where b equals 2
x = 1; // unnecessary, meant to confuse
y = 3;
var unknown = 5;
console.log(fn(unknown)); // y=3,b=2,c=5, total = 10
I'm trying to execute this piece of javascript code
(function() {
var z = '';
var b = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
for (var i = 0; i < b.length; i += 2) {
z = z + parseInt(b.substring(i, i + 2), 16) + ',';
}
z = z.substring(0, z.length - 1);
eval(eval('String.fromCharCode(' + z + ')'));
})();
but I got this error:
undefined:1: ReferenceError: document is not defined
If I assign the function to a variable, I haven't neither error nor result.
var a = function() {
var z = '';
var b = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
for (var i = 0; i < b.length; i += 2) {
z = z + parseInt(b.substring(i, i + 2), 16) + ',';
}
z = z.substring(0, z.length - 1);
eval(eval('String.fromCharCode(' + z + ')'));
};
Have you got any idea on how run this script with J2V8?
Thank you in advance
I'll be honest, I don't know what the JS is supposed to do. You have an eval wrapped in an eval, and the function has no return statement. Plus, xxxxx doesn't appear to be a valid input.
Having said all that, if I remove the wrapped eval, use a number for the the variable b and return the result, it works fine for me.
#Test
public void testExample2() {
String jsCode = "(function() {\n"
+ "var z = '';\n"
+ "var b = '12345678';\n"
+ "for (var i = 0; i < b.length; i += 2) {\n"
+ " z = z + parseInt(b.substring(i, i + 2), 16) + ',';\n"
+ "}\n"
+ "z = z.substring(0, z.length - 1);\n"
+ "return eval('String.fromCharCode(' + z + ')');\n"
+ "})();";
Object result = v8.executeScript(jsCode);
System.out.println(result);
}
Let's say I have a string (like a fraction);
var num = "1/2";
Why does this work:
var y = num.split(' ');
if (y.length > 1) {
var z = y[1].split('/');
var a = (+y[0] + (z[0] / z[1]));
num = a;
} else {
z = y[0].split('/');
if (z.length > 1) {
a = (z[0] / z[1]);
num = a;
}
}
alert(num); //The alert box shows my variable now as a decimal.
And this doesn't:
function parseFractions(x) {
var y = x.split(' ');
if (y.length > 1) {
var z = y[1].split('/');
var a = (+y[0] + (z[0] / z[1]));
x = a;
} else {
z = y[0].split('/');
if (z.length > 1) {
a = (z[0] / z[1]);
x = a;
}
}
}
parseFractions(num); //Here I call my function.
alert(num);
//The alert box does not even show up. The fraction isn't converted into a decimal.
It's basically the same thing, the only difference is that in the 2nd one I tried to make that into a function so I don't have to repeat those lines of code every time I try to convert fractions into decimals.
So what am I doing wrong? Is what I am trying to accomplish possible with a function? Any light into this issue is appreciated!
the value of num is not updated, it is not passed by reference.
function parseFractions(x) {
var y = x.split(' ');
if (y.length > 1) {
var z = y[1].split('/');
var a = (+y[0] + (z[0] / z[1]));
x = a;
} else {
z = y[0].split('/');
if (z.length > 1) {
a = (z[0] / z[1]);
x = a;
}
}
return x;
}
num = parseFractions(num); //set num with the value return from the method
alert(num);
You need to return your value
Add a return to the end of your function
return x;
Adn then call the function with
alert(parseFractions(num)); //Here I call my function.
This bit of code doesn't seem to work.
for(var b = 0; b < wallPoints.length-1; b++)
{
wallPoints[b].xPos = wallPoints[b]-1;
}
function Point(x,y)
{
this.xPos = x;
this.yPos = y;
}
wallPoints is an array of Points
The code doesn't return any errors, it just makes all my code stop executing. This is my first time using JavaScript, so this is probably a very stupid mistake.
What are you trying to do -- shift each point by one in the x axis? You need to reference the property on the right hand side of the assignment as well.
for(var b = 0; b < wallPoints.length; b++)
{
wallPoints[b].xPos = wallPoints[b].xPos - 1;
}
or do you want to propagate the x axis from one point to another
for(var b = 1; b < wallPoints.length; b++)
{
wallPoints[b].xPos = wallPoints[b-1].xPos;
}
In the latter case, you'll need to figure out what to do with the first point. Note the change in the termination condition (and start condition in the second case).
EDIT: Here's my test code:
<html>
<head>
<title>Point</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var wallPoints = new Array();
wallPoints[0] = new Point(0,10);
wallPoints[1] = new Point(600,10);
wallPoints[2] = new Point(650,10);
var content = $('#content');
content.append('<h2>Before</h2>');
for(var b = 0; b < wallPoints.length; b++)
{
content.append('<p> x = ' + wallPoints[b].xPos + ', y = ' + wallPoints[b].yPos + '</p>' );
wallPoints[b].xPos = wallPoints[b].xPos-1;
}
content.append('<h2>After</h2>');
for(var b = 0; b < wallPoints.length; b++)
{
content.append('<p> x = ' + wallPoints[b].xPos + ', y = ' + wallPoints[b].yPos + '</p>' );
}
function Point(x,y)
{
this.xPos = x;
this.yPos = y;
}
});
</script>
</head>
<body>
<div id="content">
</div>
</body>
</html>
You're assigning an X variable equal to a point. Not an option:
for(var b = 0; b < wallPoints.length-1; b++)
{
wallPoints[b].xPos = wallPoints[b]-1;
}
instead try this:
for(var b = 0; b < wallPoints.length-1; b++)
{
wallPoints[b].xPos = wallPoints[b].xPos - 1;
}