tried reverse array but reverse last item instead [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 2 years ago.
Improve this question
I tried to reverse array items, but it reverse the last item not array. I also posted the expected output. Hope it will make easy to understand.
var color = ("red", "green", "blue");
function printReverse(str) {
for (var i = str.length - 1; i >= 0; i--) {
console.log(str[i]);
}
}
printReverse(color);
/*
output
e
u
l
b
*/

You define arrays with this statement: ["red","green","blue"], not this: ("red","green","blue"):
var color = ["red","green","blue"];
function printReverse(str){
for ( var i = str.length-1; i>= 0; i-- ){
console.log(str[i]);
}
}
printReverse(color);

var color = ["red","green","blue"];
var rev_color = color.reverse()

Related

Javascript function causing error not defined [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 6 years ago.
Improve this question
I have a simple function that seems to be causing an 'Uncaught ReferenceError: arraySort is not defined' whenever the function is called, in this case by a button and i cant see why any help would be brilliant.
Javascript
<script language="javascript">
var unsorted = ["Printer","Tablet","Router"];
var alphaOrder = [" ","A","a","B","b","C","c","D","d","E","e","F","f","G","g", //15
"H","h","I","i","J","j","K","k","L","l","M","m","N","n","O", //30
"o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v", //45
"W","w","X","x","Y","y","Z","z","0","1","2","3","4","5","6", //60
"7","8","9","'","?","!",".","\"","<",">","#",",","#","~","=", //75
"+","-","_","/","\\"];
function arraySort(array){
var sortedArray = [];
var letterNum = 0;
var numArray = [];
function letterToNum(){
for (var elementNum = 0; elementNum < array.length; elementNum++;){
for (var alphaNum = 0; alphaNum < alphaOrder.length; alphaNum++;){
numArray[elementNum] = alphaOrder.indexOf(array[elementNum][letterNum]);
document.getElementById('tester1').innerHTML = numArray;
}
}
}
}
</script>
HTML
<button type = "button" onclick = "arraySort(unsorted)">Sort</button>
Remove the semicolon from the end of your loops.
for (var elementNum = 0; elementNum < array.length; elementNum++) {
for (var alphaNum = 0; alphaNum < alphaOrder.length; alphaNum++) {
}
Few suggestions here
Wrap your logic inside script by window.onload
Don't mix your markup and javascript.
Try binding events at javascript end
Follow the above suggestions, your error will be fixed.

JavaScript Array[i] is not defined [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 6 years ago.
Improve this question
i will have this code in javascript
Players = [
["івлавіл", 0, "PersonId0"],
["івлавіл", 0, "PersonId1"],
["івлавіл", 0, "PersonId2"]
];
var boys = 0,
girls = 0,
counter = 0,
ip,
PlayersCount = Players.length;
for (ip = 0; ip < PlayersCount+1; ip++) {
if (Players[ip][1] == 0) {
boys++;
}
else if (Players[ip][1] == 1) {
girls++;
}
};
When i run this, i have this error "TypeError: Players[ip] is undefined".
ip < PlayersCount+1
Get rid of the +1. You're trying to read an extra item from the array.
I'm debug this error alone;
i need change
for(ip = 0; ip < PlayersCount+1; ip++)
to
for(ip = 0; ip < PlayersCount; ip++)

JavaScript json for-loop Syntax Error [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 7 years ago.
Improve this question
I am trying to eliminate a chunk of variables:
var security = [
{ name: 'X-Powered-By',
option: '' }
, { name: 'x-frame-options',
option: file.get('headers.xFrameOptions') }
, { name: 'X-XSS-Protection',
option: file.get('headers.xXSSProtection') }
, { // AND SO ON...}
]
That is looped with:
// Add Content Security Rules to the header
for(var i = 0; i < security.length; i++) {
res.setHeader(security[i].name, security[i].option);
}
In order to eliminate all those variables, I am trying to edit the for-loop in the following way:
for(var i = 0; i < file.get('headers.length; i++') {
res.setHeader(headers[i].name);
}
I am getting a syntax error and I am not sure if I am doing it correctly. An example would be very appreciated.
you have problems with your closing brackets and quotes, should be:
for(var i = 0; i < file.get('headers.length') ; i++) {
res.setHeader(headers[i].name);
}

Array.lenght returns 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 8 years ago.
Improve this question
I wish to get the length of an array so I can use it further in another function but it returns undefined. This piece of code gets a file, opens it and splits it for each new line. I'm trying to get the length of the array but returns undefined.
function readBatFile(bfile){
var rawFile1 = new XMLHttpRequest();
rawFile1.open("GET", bfile, false);
rawFile1.onreadystatechange = function ()
{
if(rawFile1.readyState === 4)
{
if(rawFile1.status === 200 || rawFile.status === 0)
{
var allCode = rawFile1.responseText;
var ary = new Array;
ary = allCode.split(/.*\n/gm);
var rcount = ary.lenght;
document.getElementById('test').innerHTML = rcount;
}
}
};
rawFile1.send(null);
}
It is spelled length not lenght.
It should be:
var rcount = ary.length;

JS: Why FOR LOOP loops once? [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 7 years ago.
Improve this question
Why I get only one alert after run this code:
var poly = function()
{
this.disp = function()
{
for(var i=0; i<6; i++); //And I already found my problem. It is this ';'
{
alert('The number is '+i); //I get one alert: 'The number is 6'
}
}
}
test = new poly();
test.disp();
Thanks for any help!
This code works. Probably, in your real code, it looks like this:
var poly = function()
{
this.disp = function()
{
for(var i=0; i<6; i++)
{
}
alert('The number is '+i); //I get one alert: 'The number is 6'
}
}
That will cause i to loop from 0 to 6, after which it is alerted once.

Categories