Uncaught SyntaxError: missing ) after argument list in loop [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 7 years ago.
Improve this question
I keep getting the same error in my code on line 21.
if(typeof jQuery === undefined){
throw "jQuery is required for sapphire to work. Didn't you read the README?";
}
(function ( $ ) {
$.fn.slider = (function(options,images) {
var settings = $.extend({
slideCount: 4,
animationType:"none",
slideDuration:2000,
sliderSize:1100,
looptimes:300000000000000000000000000000000000000000
},options);
for(var i = 0; i<options.looptimes; i++){
var j = 0;
$("#sapphire-slide").append("<img src='"+images[j]+"'>");
setTimeout(function() {
if(j<options.slideCount-1)
j++;
}else if(j===options.slideCount-1){
j=0;
}
},options.slideDuration);
}
);
})( jQuery );
I am not sure what is causing this error, and it looks like perfectly fine syntax to me. Thanks!

you've got an extra closing brace for the if in the function that you're passing to setTimeout:
if (j < options.slideCount - 1)
j++;
// the errant closing brace is on the next line:
}
else if(j === options.slideCount - 1)
{
j = 0;
}
Or, as others have mentioned, add an opening brace to the if to make a proper block:
if (j < options.slideCount - 1)
{ // you need this opening brace
j++;
}
else if(j === options.slideCount - 1)
{
j = 0;
}

You are missing the opening { on the if and you are missing the closing } on the second last line, before the ):
(function ($) {
$.fn.slider = (function (options, images) {
var settings = $.extend({
slideCount: 4,
animationType: "none",
slideDuration: 2000,
sliderSize: 1100,
looptimes: 300000000000000000000000000000000000000000
}, options);
for (var i = 0; i < options.looptimes; i++) {
var j = 0;
$("#sapphire-slide").append("<img src='" + images[j] + "'>");
setTimeout(function () {
if (j < options.slideCount - 1) { // <--- add this {
j++;
} else if (j === options.slideCount - 1) {
j = 0;
}
}, options.slideDuration);
}
}); // <--- add a } on this line
})(jQuery);
Note that fixing this sort of thing is just a matter of counting brackets and parentheses and making sure they're balanced.

Related

Create a function that takes a word and returns true if the word has two consecutive identical letters [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
// Create a function that takes a word and returns true if the word has two consecutive identical letters.
What am I doing wrong?
module.exports = (word) => {
for (let i = 0; i <= word.length; i++) {
for (let j = i + 1; j <= word.length; j++) {
if (word[j] == word[i]) {
return true;
}
} return false;
}
};
You can do this with only 1 loop.
function hasConsecutiveIdenticalLetters(word){
for (let i = 1; i < word.length; i++) {
if (word[i-1] === word[i]) {
return true;
}
}
return false;
}
You can also achieve this like below using some
const hasConsecutiveIdenticalLetters = (word: string) => (word).split('').some((letter, index) => letter === word[index + 1]);

Generating id's and pushing them into an 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 2 years ago.
Improve this question
I have a function generating color id's:
const hexaColor = () => {
let str = '0123456789abcdef'
let color = ''
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * str.length)
color += str[index]
}
return '#' + color
}
And then I'm using another function, as I want to have an array of six generated colors:
const colorArray= ()=> {
let colors = []
for (let i=0; i>=6; i++ ){
colors.push(hexaColor[i])
console.log(colors)
}
return colors;
}
console.log(colorArray())
however, what I see in a console is just an empty array. What am I doing wrong? Any help appreciated.
You have a code error
The loop did not run at all
The condition will never be meti>=6
Change the condition at the top of the loop
for (let i=0; i>=6; i++ ){
This is how it works
for (let i=0; i<=6; i++ ){
also this line fix to
colors.push(hexaColor()[i])
The issue is that in your loop you said when i >= 6, but it's i <=6 because i starts from 0.
const hexaColor = () => {
let str = '0123456789abcdef'
let color = ''
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * str.length)
color += str[index]
}
return '#' + color;
}
const colorArray = ()=> {
let colors = []
for (let i=0; i<=6; i++ ){
colors.push(hexaColor());
}
return colors;
}
console.log(colorArray())

Uncaught SyntaxError: missing ; after for-loop initializer, but I can't see where it's missing [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 just started learning javascript, and I want to make an online lcm hcf interactive teaching tool, but I keep getting this error pointed out below, and I can't see where it's missing
$("document").ready(function(){
function start() {
var firstNum = document.getElementById("first-num");
var secondNum = document.getElementById("second-num");
var primeList1 = [];
var primeList2 = [];
var primes = [];
var maxPrime = math.max(firstNum, secondNum) / 2 + 1;
**for (int num = 2; num < maxPrime; num++) {** <--- this line has the error
for (int i = 2; num < i; i++)
if (num % i == 0) {
break;
} else {
primes.add(num);
};
};
};
There is no int keyword in JavaScript.
You need to use var or let to declare and initialize your num and i variables
for (let num = 2; num < maxPrime; num++) {
for (let i = 2; num < i; i++)

Javascript word counter [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 years ago.
Improve this question
I wanted to make a basic word counter based on the amount of whitespaces in a sentence, but for some reason it doesn't work.
function countWords(str) {
if (typeof str == "string" && str.length > 0) {
var counter = 1;
for (var i; i < str.length; i++) {
if (str[i] === " ") {
counter += 1;
}
}
return counter;
} else {
throw Error("You must input a string.");
}
}
console.log(countWords("hello World"));
This throws 1 instead of 2.
You shouldn't use a loop for this. You would rather just split the string by space and take the resulting array's length
let countWords = str => str.split(' ').length;
console.log(countWords("foo bar"));
Initialize i to zero.
Replace for (var i; with for (var i=0;
You must initilize the counter inside the for, like var i = 0; here is your code
function countWords(str) {
if (typeof str=="string" && str.length>0) {
var counter=1;
for (var i;i<str.length;i++) {
if (str[i]===" ") {
counter+=1;
}
}
return counter;
}
else {
throw Error("You must input a string.");
}
}
countWords("hello World");
Or you can count words with str.split(" ").length
Your for loop was wrong
function countWords(str) {
if (typeof str=="string" && str.length>0) {
var counter=1;
for (var i = 0;i<str.length;i++) {
if (str[i]===" ") {
counter+=1;
}
}
return counter;
}
else {
throw Error("You must input a string.");
}
}
var str = "hello World this is me";
console.log(countWords(str));

A quite weird 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 8 years ago.
Improve this question
Here is my problem, I'm programming a game and I need to use a very simple array, the problem is that I create it, everything's ok, and the next line, the values of an entire line in the array change and I don't understand why, here is my code :
var ciblesPossibles = new Array();
for (var i = 0; i < 2; i++) {
for (var j = 0; j < 5; j++) {
ciblesPossibles[i,j] = 1 + portee(i, j, ID, pos, carte.effet.portee) + duBonCote(i, ID, carte.effet.ciblesLegales);
}
console.log('1/ ciblesPossibles['+i+',x] = ' + ciblesPossibles[i,0] + ciblesPossibles[i,1] + ciblesPossibles[i,2] + ciblesPossibles[i,3] + ciblesPossibles[i,4]);
}
for (i = 0; i < 2; i++) {
console.log('2/ ciblesPossibles['+i+',x] = ' + ciblesPossibles[i,0] + ciblesPossibles[i,1] + ciblesPossibles[i,2] + ciblesPossibles[i,3] + ciblesPossibles[i,4]);
}
var max = maxTab(ciblesPossibles);
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
ciblesPossibles[i,j] = Math.floor(ciblesPossibles[i,j] / max);
console.log(ciblesPossibles[i,j]);
}
}
portee() and duBonCote() are two functions which return just 1 or 0.
When I'm at the console.log('/1...'), I have something like 33222 and 22211 (it's what I want), but when I'm at the console.log('/2...'), I have 22211 and 22211 ... What can make the first line change in my array ?
Regards
Two dimensional arrays are accessed as a[i][j], not a[i,j].
The latter will be treated as a use of the comma operator, and evaluates to just a[j], i.e. a one-dimensional matrix.
You'll be wanting something more like:
var ciblesPossibles = []; // create array to hold rows - NB: not "new Array():
for (var i = 0; i < 2; i++) {
ciblesPossibles[i] = []; // create the individual row
for (var j = 0; j < 5; j++) {
ciblesPossibles[i][j] = ...
}
}

Categories