Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
var test = "abcdefghijklmnopqrstuvwxyz";
for(i = 0; i < test.length; i++) {
alert(test.substring(i,1));
}
I expected each alert to return each letter of the alphabet individually.
Instead, the first 5 alerts displayed as follows. Why?
a
b
bc
bcd
bcde
var test = "abcdefghijklmnopqrstuvwxyz";
for(i = 0; i < test.length; i++) {
console.log(test.substring(i,i+1));
}
actually, it's
substring(start, end)
not
substring(start, length)
unlike substr, which is indeed, substr(start, length)
If "start" is greater than "end", this method (substring) will swap the two arguments, meaning str.substring(1,4) == str.substring(4,1).
Use:
for(i = 0; i < test.length; i++) {
alert(test[i]);
}
Related
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 3 years ago.
Improve this question
I'm supposed to get the multiplication value of the multi-dimensional array. but I am getting '1' as output whatever values being changed in array.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i];j++){
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You need to check arr[i].length in the j loop.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i].length;j++){ // you need to check arr[i].length here
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You can simply do this in two lines.
function arrayMultiplyer(arr){
let flattenedArray = arr.flat();
return flattenedArray.reduce((x, y) => x * y);
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
An alternative solution using .reduce()
function arrayMultiplier(arr) {
return arr.reduce((tot,arr2) =>
arr2.reduce((subTot, n) =>
subTot * n
, tot)
, 1);
}
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 have one string as "testaabbaccc" in this string we contain palindrome as "abba" and it's length is 4 but how can we identify this with a JavaScript code.
var string ="testaabbaccc"
Need Output as abba is palindrome and length is 4
You can use this article and modify it to your needs.
Working demo
function isPalindrome(s) {
var rev = s.split("").reverse().join("");
return s == rev;
}
function longestPalind(s) {
var maxp_length = 0,
maxp = '';
for (var i = 0; i < s.length; i++) {
var subs = s.substr(i, s.length);
for (var j = subs.length; j >= 0; j--) {
var sub_subs = subs.substr(0, j);
if (sub_subs.length <= 1)
continue;
if (isPalindrome(sub_subs)) {
if (sub_subs.length > maxp_length) {
maxp_length = sub_subs.length;
maxp = sub_subs;
}
}
}
}
return maxp;
}
console.log(longestPalind("testaabbaccc"));
console.log(longestPalind("testaabbaccc").length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
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 5 years ago.
Improve this question
for (i = 0; i < length1; i++) {
for (j = 0; j < length2; j++) {
for (k = j; k < length2; k++) {}
}
}
I just wondered... in the last loop where I initialize k to be equal to j, would it cause problems for me later on?
I'm assuming every time j increments by 1, k is set to the new value of j and then increments up to length2
You are correct that for each iteration of the j-for-loop, the innermost loop will begin at J's current value and iterate up to length2-1. If this is what you want, shouldn't be a problem.
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 6 years ago.
Improve this question
I have been working on a ceaser cipher algorithm but I haven't been able to grasp the reason why the joined array returns spaces in a peculiar state.
function rot13(str) { // LBH QVQ VG!
var string = str.split('');
var codedStr = [];
var encoded = [];
for (var k=0; k < string.length; k++){
codedStr.push(string[k].charCodeAt());
}
for(var i = 0; i < codedStr.length; i++){
if(codedStr[i] > 77 ){
codedStr[i] -= 13;
}
else if( codedStr[i] == 32 || codedStr[i] == 63){
codedStr[i] = codedStr[i];
}
else{
codedStr[i] += 13;
}
encoded.push(codedStr[i]);
}
var decode = codedStr.map(String.fromCharCode);
var result = decode.join('');
return result;
}
// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC"));
String.fromCharCode accepts multiple arguments, and map provides 3. You should use
codedStr.map(code => String.fromCharCode(code));
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 can't work out why this only loops through the array once in JavaScript. It should log the second nested array as well.
JSFiddle below and code below;
http://jsfiddle.net/HJfbT/
b = [["one", "is"],
["two", "is"]];
for (var i = 0; i < b.length; i++) {
for (var x = 0; x < b[x].length; x++) {
console.log(b[i][x]);
}
}
Because you have a typo:
// --- should be i ---v
for (var x = 0; x < b[x].length; x++) {
DEMO: http://jsfiddle.net/HJfbT/1/
Use b[i].length in the second loop.
I think is because the inner loop have the running condition with an error.
Is:
for (var x = 0; x < b[x].length; x++)
but must be:
for (var x = 0; x < b[i].length; x++)