Attempting to use a 'for' loop to build an Array [closed] - javascript

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"

Related

Push a random character from an array into another array [closed]

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

all the previously stored objects in an array are replaced with the newly inserted object at the end of array. why? [closed]

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.

Why doesn't variable update in for loop JavaScript? [closed]

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 am doing a coding challenge where the letters in a string are replaced by their partner letters, I know that there is probably a better way to do this, but I just want to know why this doesn't work. Here is the code:
function DNAStrand(dna){
var strObject = {"A":"T", "T":"A", "C":"G", "G":"C"};
let newDna = "";
for (let i=0; i < dna.lenght; i++){
newDna += strObject[dna[i]];
}
return newDna
}
the function returns an empty string or "", the value of newDna before the loop, it doesn't change.
for (let i=0; i < dna.lenght; i++){
You made a typo. Change this to dna.length.

JS Cannot read property "length" of undefined [closed]

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 5 years ago.
Improve this question
I'm trying to create an object using a given string where each word has a property stating its length.
var strings = {};
function findLongestWord(str) {
var splitStr = str.split(" ");
for (var i = 0; i <= str.length; i++){
strings[splitStr[i]] = splitStr[i].length;
}
return strings;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
I end up getting:
"TypeError": Cannot read property "length" of undefined.
If I were to replace splitStr[i].length with splitStr[0].length, the code runs properly, but of course giving me the same number for each word in the object.
Any help is appreciated, thanks.
you are looping over wrong array. you should use i < splitStr.length.
var strings = {};
function findLongestWord(str) {
var splitStr = str.split(" ");
for (var i = 0; i < splitStr.length; i++){
strings[splitStr[i]] = splitStr[i].length;
}
return strings;
}

Javascript replace exact match string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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 have the following
var string = '1,7,12,15,16,29';
if I just want to replace the number 1, i will use the following
removeStr(1);
function removeStr(str1)
{
var string = '1,7,12,15,16,29';
var newstr = string.replace(str1, '');
alert('new val is ' + newstr);
}
But doing this, will end up removing the number 1 in 12,15,16.
How do I just remove the exact match 1 in this example.
Thanks
You could use boundaries (\b) in a regexp that to match a whole word only. Changed your test string to one where your question would be applicable
function removeStr(str1)
{
var string = '11,71,12,1,16,21';
var newstr = string.replace(new RegExp("\\b"+str1+"\\b"), "");
console.log('new val is ' + newstr);
}
removeStr("1");
function replaceOne(str1, str2){
var arr = str2.split(",");
var newStr = "";
for(var i=0; i<arr.length; i++){
if(arr[i]!=str1){
newStr = (newStr=="")?arr[i]:newStr+","+arr[i];
}
}
console.log(newStr);
}
You are trying to do it on strings.
you might consider to make it an array
var string = '1,7,12,15,16,29';
var arr=string.split(",");
var newArr=arr.splice("1");
string=newArr.join(",");
console.log(string);
Hope this helps

Categories