Fatal error occurring in Javascript forming 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 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));

Related

Why is my function returning undefined, when it should return a number? [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 trying to solve challenge on one website, and my code behaves strange... It returns undefined... What is interesting: if I print value of "sum" just before return statement, it is correct value in the console... Any help would be awesome
function digital_root(n) {
let text = String(n);
let sum = 0;
for (let i = 0; i < text.length; i++) {
sum += Number(text[i]);
}
if (String(sum).length > 1) {
digital_root(sum);
} else {
return sum;
}
}
let score = digital_root(456);
Make sure to propagate the recursive return value upwards. If you call digital_root() without returning the child's result then the parent call returns undefined.
if (String(sum).length > 1) {
return digital_root(sum);
}

If statement inside a for loop doesn't working 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 2 years ago.
Improve this question
I tried to return a value inside a simple for loop if a certain condition fulfills. But it's not working.
It's a very simple code:
function c()
{
for(var i=5;i>-1;i++)
{
if(i==2)
{
return i;
}
}
}
Shouldn't there be an i-- instead of i++?
function c()
{
for(var i = 5; i > -1; i--)
{
if(i == 2)
{
return i;
}
}
}

Why does a for loop behave oddly in a JavaScript constructor? [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'm new to constructors - I'm trying to use one to create a customizable object, with this sort of code:
class test{
constructor(range) {
var start;
if(range==="a"){
start = 56;
}
else if(range==="b"){
start = 53;
}
for(var i=start; i<(start+5); i++); {
console.log(i);
//construct an array here
}
}
}
const myTest = new test("a");
But only the last loop seems to execute!
The log shows just the value 61.
You have an semicolon to early. The result is an empty statement and an additional block statement outside of the loop.
Finally you get the last value of i.
for (var i = start; i < (start + 5); i++); {
// ^

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.

Why for statement doesn`t work [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
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

Categories

Resources