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 8 years ago.
Improve this question
I have looked at some other questions and they don't seem to address the particular issue I am encountering. Basically, I want to set the key of an object with a variable and then change that variable. I am using this to dynamically increment the keys in the object.
var x = 0;
var object = {};
object[x] = 1;
x+1;
object[x] = 2;
The above will produce undefined for object[1] and object[2] and 2 for object[x]. I would like the output to be 1 for object[1] and 2 for object[2]. Is this possible?
You've written object[0] = 1; object[0] = 2. If you want the keys to match the values, use matching keys and values.
Hint: Start with x = 1 instead of x = 0.
Next, you've written x+1;, which is a do-nothing statement. If you want to add one to x and assign the result to x, you need to *assign the result back to x`. Any of these will do:
x = x + 1
x += 1
x++
Try this:
var x = 1;
var object = {};
object[x] = 1;
x=x+1;
object[x] = 2;
You don't give the new value to x when adding 1 and also initialize x with 1 not 0 to get your result.
Related
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 21 days ago.
Improve this question
const propCSS = resposta['propCSS'];
const listData = getList(cssList);
listData.lista = propCSS;
for (let i = 1; i <= list.length; i--) {
list.push(` "$ {listData}" `);
console.log(lista);
}
I'm trying to add an item to an array in a JSON file, but the result only returns me undefined at position 10.
It seems like an error in your for loop implementation.
You are loopping trough an array starting from the position 1 (let i = 1) till the end of the array, but you are descreasing the iterator (i--).
So your loop goes like: list[1] then list[0] then list[-1]... and an array can't have negative indexes.
You need to increase you iterator (i++) or start your loop from the end of the array till the begining: for (let i = list.length-1; i === 0; i--) { //push() }
Also, is this all of your code? If it is, then list.length and list.push() won't work because there is no list declared. Same thing for the console.log(lista), there's no lista declared.
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 4 months ago.
Improve this question
var legalCharactersForSaveCode = ["B","C","D","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "+", "/",];
var saveCode = [];
function generateSaveCode() {
for(var c = 0; c > 30; c++){
v = Math.round(Math.random() * legalCharactersForSaveCode.length);
saveCode.push(legalCharactersForSaveCode[v]);
}
saveCode.push(store.get("money"));
alert(saveCode);
}
I'm trying to make it so it pushes a random character.
When I ask it to alert the saveCode array all it does is alert the money.
(I'm using store.js)
I was expecting for it to push a random characters from the legalCharacters array into the saveCode array.
Your "for" loop does not work as the condition is c > 30, and should be c < 30
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 2 years ago.
Improve this question
I have been inserting objects in the array one by one from an array of objects. But whenever I insert a new object, previous objects are automatically replaced. I am executing the following code block:
let slotsArray = [];
let slotsObj = {};
try {
let slotsData = await Slots.find({ author: req.user._id });
for (let j = 0; j < slotsData.length; j++) {
slotsObj.teacher = `${slotsData[j].teacherName}`;
slotsObj.sections = [`${slotsData[j].session}-${slotsData[j].section}`];
slotsObj.subject = `${slotsData[j].subjectName}`;
slotsObj.numLectures = `${slotsData[j].contactHours}`;
slotsObj.numLabs = null;
slotsArray.push(slotsObj);
}
Move let slotsObj = {}; inside the for loop, so slotsObj will be a new reference at each iteration.
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 was testing my answer to another SO question and came across this weird behavior, for the life of me I don't know what is up.
Code:
function translateLetter(input) {
const untranslated = "abcdefghijklmnopqrstuvwxyz";
const translated = "zyxwvutsrqponmlkjihgfedcba";
var i = untranslated.indexOf(input);
console.log(i);
return translated.substring(i,1);
}
console.log(translateLetter("a"));
console.log(translateLetter("b"));
console.log(translateLetter("c"));
Expected output:
0
z
1
y
2
x
Actual output:
0
z
1
<--- WTH?
2
y <--- WTF?
Code on JSFiddle
If speed is important, I'd use an object to do your lookup.
eg.
var translateLetter= {a:'z',b:'y'... etc }
and then you can simply do ->
console.log(translateLetter['a'])
Use .substr() for a length. .substring() takes a position.
The first empty space is a null because you have passed "b" in the function on which indexOf function returned 1 which is then set to variable "i" and i is then used in substring function and substring fuction return the value which is in the middle including the first and excluding the last index of the values given so substring got (1,1) and 1,1 are pointing the same index so it returned null.
same for the second.
substring(Begin index inclusive,last index exclusive);
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 am attempting to build an array whose entries are the letters of any given word. I though I had written a clever bit of code, but it doesn't work in the slightest! And feedback or help would be greatly appreciated:
var inputWord = prompt("PALINDROME CHECKER:");
var numberOfLetters = inputWord.length;
var letters = [];
for(i=0; i++; i<numberOfLetters){
letters[i] = inputWord.substring(i,i+1);
};
Thanks,
CPR
If you want an array with the letters of a string, just split the string with no pattern:
var string = "My string is this";
var array = string.split("");
console.log(array);
Your for loop is wrong. Try:
var inputWord = prompt("PALINDROME CHECKER:");
var numberOfLetters = inputWord.length;
var letters = [];
for(i=0; i<numberOfLetters; i++){
letters[i] = inputWord.substring(i,i+1);
};
The order of the for loop parameters should be iterator, then condition, then action - basically, "for my variable i, if i is less than the number of letters, then increment i"