Why for statement doesn`t work [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
function main (str, d){
var myStr = str.split(d);
for(var x=0; x<myStr.lenght; x++){
console.log(myStr[x]);
}
}
console.log(main('one-two-three-four-five','-'));
It should print : one two three four five on different lines but it doesn`t.Can u guys help me with this problem? Thank you!

Use Array#length for the length of an array.
function main (str, d){
var myStr = str.split(d);
for (var x = 0; x < myStr.length; x++) {
// ^^^^^^
console.log(myStr[x]);
}
}
console.log(main('one-two-three-four-five', '-')); // this returns undefined at the end of the console

Related

Fatal error occurring in Javascript forming 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 1 year ago.
Improve this question
I want to make a function that forms an array based on the user input so I write the javascript as below but it only returns a fatal error. What is wrong with this code? I try to match with the book's code but I don't find anything particularly different so I came to the StackOverflow. The code is as follows
function arrayForm(start, limit)
{
let array = [];
for (start <= limit; start++;)
{
array.push(start);
}
return array;
}
console.log(arrayForm(1,10));
try
{
let array = [];
for (let i = start; i <= limit; i++)
{
array.push(i);
}
return array;
}
console.log(arrayForm(1,10));

Why doesn't this for loop work and my variable equals to zero? [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
This is my first post on Stack Overflow, thanks in advance for answering me!
I need to count how many dots and exclamation marks there are in a single paragraph. I am using a for loop for this.
const storySigns = story.split('');
let puncCount = 0;
for (let i = 0; i < storySigns.length; i++)
{
if (i === '.' || i === '!') {
puncCount += 1;
}
};
Why does the output of puncCount equals 0?
I is a counter here its values is 0 then 1 then 2 then 3 until n
It will never be equal to '.' Or '!'

Create a JavaScript function writing odd numbers between 0 and 15001 [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 am trying to improve my ability to create efficient algorithms everyday and I am facing an issue with this one... I want to create a JavaScript function loop to write only odd numbers between 0 and 15000:
function Nowork() {
for(x = 1; x < 15001; x+2) {
document.write(x);
}
}
Nowork();
This one doesn't work (also I know document.write should be written only for testing and debugging), instead I know that one works but it only write the even numbers:
function Works() {
for(x = 1; x < 15001; x++) {
document.write(x);
}
}
Works();
Does anyone have an idea how to do that and also explain to me why my first function doesn't work?
use this:
function Nowork() {
for(x = 1; x < 15001; x=x+2) {
document.write(x);
}
}
Nowork();
you cant use +2 like that.

Map each value of an array to a given function to return a new array in 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 6 years ago.
Improve this question
The code below throws the following exeption:
Uncaught TypeError: someFunction(...) is not a function
What is causing this exception to be thrown?
map = function (someList, someFunction){
var result =[];
for (x = 0; x < someList.length; x++ ){
result.push(someFunction(someList[x])());
}
return result;
};
map([1,2,3,4], function(num){
return num * 10;
});
The problem is in this line
result.push(someFunction(someList[x])());
^^
This extra parentheses are redundant. You are already calling the function someFunction by saying someFunction(someList[x]). By adding these extra parentheses, you are basically trying to call the return value of someFunction(someList[x]), which is a number here, not a function.

Error in for-loop: FireFox claims missing parenthesis, but is not true. Issue with generic selector? [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 9 years ago.
Improve this question
I'm getting this error:
SyntaxError: missing ) after argument list
in my firefox console, pointing to the indicated line in this for loop:
for (var x = 0; x < 16; x++) {
for (var y = 0; y < 8; y++) {
if ($('#'x+','+y).hasClass('selected')) { //<---error
cell[x,y,0] = 'alive';
} else {
cell[x,y,0] = 'dead';
}
}
}
It seems FireFox is wrong with its claim that there's a missing paranthesis. Perhaps there is a problem with the selector on the line of the error? The generic selector is meant to point to a large number of html elements with ids in the form "x,y". Example: "12,4"
Parse error in
$('#'x+','+y).
You're missing a +. It should be:
$('#' + x + ',' + y).
with whitespace for readability.

Categories

Resources