My function needs to call a number of index values based on the users input (1-5) based on job duties they want to see and then call from the array the index values, so if the user inputs 3, index values 0,1,2 need to be written. Currently it only writes the one associated with the input not all below that input. This is what I have so far:
function jobduties() {
var x = document.getElementById("input").value;
var y = x-1;
var duties = ["Sales", "Customer Service", "Management", "Driving", "Cleaning"];
var z = " ";
while(x > y){
z += z + duties[y];
y++;
document.getElementById("print").innerHTML= z;
}
}
The problem is that you're setting y = x-1, so if the user enters 3 (x=3), y will be 2 (x-1)... In your while you start with y = 2, you add that to z, and then increase y, so you are only evaluating y = 2, since the next one is y=3, and that doesn't satisfy your condition (x > y)
Try these changes...
change var y = x - 1;
for var y = 0;
And also remove the + where you edit the z variable.
Edit: By the way, I just answer following the method you choosed, but I strongly recommend you to use a FOR loop instead, much easier..
Related
Stupid question,
but if I have code like this:
var x;
var y;
var z = x + y;
And then through the program I update the variables for x and y, but when I use the console to check var z, it gives me NaN. So the program is doing that calculation once at the beginning and not updating Z as it continues. So what's the solution to keep checking Z for the variable changes? Do I have to do a loop with setInterval to keep checking the new updated Z variable? Thanks
Yeah, your comment is right. JavaScript doesn't provide a way to create a live binging.
So what's the solution to keep checking Z for the variable changes?
All depends on what you are trying to do.
What you think about the code:
var x = 1;
var y = 2;
var z = x + y;
console.log("The value of z variable: ", z);
PS. Preferably you should use let:
let x = 1;
let y = 2;
let z = x + y;
console.log("The value of z variable: ", z);
Below is my code
function generateGrid(spacing, boundBox, geometry) {
console.log(spacing);
console.log(boundBox);
var grid = [];
console.log(spacing);
for (var x = boundBox[0]; x < boundBox[2]; x = x + spacing) {
for (var y = boundBox[1]; y < boundBox[3]; y = y + spacing) {
if(geometry.intersectsCoordinate([x, y])) grid.push([x, y]);
console.log(boundBox[3] - y)
}
console.log(boundBox[1] - x)
}
console.log(grid);
}
If spacing is replaced by a number like 10000 the for loop executes fine.
From your Console screenshot it looks like the passed in argument is the string "10000" rather than the number 10000.
Either check the code that's calling your function, or convert to an integer inside the function, for example by using parseInt(spacing).
As a tip to help with spotting any similar issues in the future, Chrome's console.log shows numeric values in blue and string values in black.
x is a number so use String(x) so you use operator + between two strings, that would give you "15" + "1" = "151", but that is probably not what you wanted
I am trying to make a program that sums every number given as parameter. To do so, I wrote the following code:
var x = 0;
var i = 2;
while (isNaN(+process.argv[i + 1]) == false){
x = +process.argv[i] + +process.argv[i + 1];
i++;
}
console.log(x);
The problem is that the code I wrote sums only the 2 last parameter.
I launch my code using node sumArgs.js 1 2 3
and it returns 5.
What is the problem with my code and why isn't it working as planned ?
What is happening every time you loop through, it is taking the current parameter, and the next, and setting x to equal the sum of those.
x needs to be added to, not set. You can do this either:
x += process.argv[i]
or
x = x + process.argv[i]
I'm also not sure why you are adding 2 arguments each loop, as this will cause the sum to be incorrect at the end (unless you increment i twice each loop).
I should note that map reducing it, as in another comment, wouldn't work as the first 2 arguments would not be parameters passed to the program, they would be "node" and "program.js".
var x = 0;
var i = 2;
while (isNaN(+process.argv[i]) == false){
x = x + process.argv[i];
i++;
}
console.log(x);
However, what you could do is use slice:
var sum = process.argv.slice(2).reduce(function(previousValue, currentValue) {
return previousValue + currentValue;
});
In javascript I get two numbers let's call them x & y and an array of integers with random int's from 0 - 10 arrayints.
x is the number I'm trying to get by combining y with any of the numbers in arrayints.
for example: lets say x = 8 and y = 3 and arrayints consists of numbers arrayints(1,7,2,7,4,5)
so x could equal = y + 5
or
x = y + 1 + 4
All the values in x, y and arrayints will be random and always <= 10.
Please advise if more information is needed and everything will be in javascript or jquery no fuss as far as my code goes I will copy and paste but it will just be one blob of incromprehensible letters which are giving me an headache.
function makex(x,y) {
//this is how I get the array of random ints <=10
$("#div").children().each(function(n, i) {
var id = parseInt(this.id+"");
});
}
Here's a recursive solution that returns an array of the integers that add up to x, including y (or an empty array if it doesn't exist). If you want to exclude y, then feel free to make a wrapper for this.
function make(x, y, intOptions) {
var z = x - y
if (intOptions.indexOf(z) !== -1) {
return [y, z];
} else if (intOptions.length > 1){
var i = intOptions.length;
var ans;
while (i--) {
ans = make(z, intOptions[i], intOptions.slice(0, i))
if (ans.length) {
return [y].concat(ans);
}
}
}
return [];
}
I have a list of data in javascript that looks like this:
[[152, 48, 'http://www.google.com'],
[198, 47, 'http://www.stackoverflow.com'],
[199, 45, 'http://www.apple.com']]
I am using flot to create a plot, and am trying to pass this third value to access a hyperlink from the point. As such, I am trying to lookup the third value of each list by using the first two as the lookup keys (i.e., [[x,y,hyperlink],[x2,y2,hyperlink2]], click on a point, then use the appropriate (x,y) to find the corresponding hyperlink)
Is there anyway to do this, or do I need to pass some dictionaries for x and y to javascript, then find the common variable from the two lists that were looked up? In python I know you could do a filter of list on the x value with itemgetter, then lookup a link corresponding to the y value. But I know almost nothing about js, so could a solution to ID-ing with (x,y) be given, or if not possible or advised, then a solution to taking two lists of (from x and y vals) and find a common value (if multiple, just one, anyone)?
You can make use of the Array .filter() method to figure out if any elements match the supplied x and y. (Note that IE didn't support .filter() until version 9, but MDN has a shim that you can include).
var data = [[152, 48, 'http://www.google.com'],
[198, 47, 'http://www.stackoverflow.com'],
[199, 45, 'http://www.apple.com']];
function getURLForPoint1(x, y) {
var p = data.filter(function(el) {
return (el[0] === x && el[1] === y);
});
if (p.length === 1) return p[0][2];
// else 0 or more than 1 elements mathced so return undefined
}
Alternatively you can create a dictionary object up front and then do future lookups from the dictionary:
var getURLForPoint2 = function() {
var dataDictionary = {}, i;
for (i = 0; i < data.length; i++)
dataDictionary[data[i][0]+" "+data[i][1]] = data[i][2];
return function(x, y) {
return dataDictionary[x + " " + y];
};
}();
Either way I've coded it so that if you ask for a point that isn't in the list you'll get undefined back, but obviously you can change that to return an empty string or throw an exception or whatever you like.
alert(getURLForPoint1(198, 47)); // 'http://www.stackoverflow.com'
alert(getURLForPoint2(198, 47)); // 'http://www.stackoverflow.com'
alert(getURLForPoint2(4, 5)); // undefined
Demo of both: http://jsfiddle.net/WdSAz/
Sorry, no shortcut way to do it in js except to just loop through the list and find the one that has the matching "x" and "y" value.
However, depending on how large your list is (and whether or not this list will be used for something else...) you could restructure the data to make it more efficient. For instance, do a structure like (assumed possible to have for instance x1, y1 vs x1, y2)
x1 > y1 > url
x1 > y2 > url
x2 > y1 > url
etc...
then you can immediately jump to the 2nd lvl "y" list by the "x" index, and the only looping would be how many "y" values share the same "x" value
edit:
actually if you wanna take it a step further with reorganizing the data, you could do something like this:
<script type='text/javascript'>
var list = {
1 : {
1 : 'foobar 1,1',
2 : 'foobar 1,2'
},
2 : {
1 : 'foobar 2,1',
2 : 'foobar 2,2'
},
};
</script>
which will allow you to do for instance this
var x = 1;
var y = 2;
alert(list[x][y]);
somthing like this maybe
var findX = 198
var findY = 47
var targetUrl
for (var i=0; i<arr.length; i++)
{
for (var j=0; j<arr[i].length; j++)
{
if (findX = j[0] && findY == j[1])
{
targetUrl = j[2]
}
}
}