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 know this is very simple question, but I don't know why it only giving same output.
I'm trying to randomize 4 numbers from 1 to 100
I'm very new to javascript
this is my code:
function myFunction() {
var text = "",
i;
var x = Math.floor((Math.random() * 100) + 1);
for (i = 1; i <= 4; i++) {
text += " number " + x;
}
document.getElementById("rand").innerHTML = text;
}
<button onclick="myFunction()">Generate</button>
<p id="rand"></p>
You are calling your randomize part of code outside the for loop. It should be recalculated on every step of the loop. Something like this
function myFunction() {
var text = "",
i;
for (i = 1; i <= 4; i++) {
var x = Math.floor((Math.random() * 100) + 1);
text += " number " + x;
}
document.getElementById("rand").innerHTML = text;
}
<button onclick="myFunction()">Generate</button>
<p id="rand"></p>
function myFunction() {
var text = "";
var i = 0;
randNos = [];
while(i<4) {
x = rand();
if(randNos.indexOf(x) === -1) {
text += " number " + x ;
randNos.push(x);
i++
}
}
document.getElementById("rand").innerHTML = text;
}
function rand() {
return Math.floor((Math.random() * 100) + 1);
}
You were trying to print the same random no 4 times. You just need to move your random no line inside the loop
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 9 months ago.
Improve this question
I am new to JavaScript and I try to write a decoder to caesar cipher. But my code doesn't work, and there is no errors shown and it doesn't stop.
let word = "привет";
let shift = 3;
let result = "";
for (let i = 0; i < word.legth; i++){
if ('А'.charCodeAt(0) <= word[i].charCodeAt(0) <= 'Я'.charCodeAt(0)) {
let char = ((word[i].charCodeAt(0) + shift - 'А'.charCodeAt(0)) % 32) + 'А'.charCodeAt(0)
result = result + String.fromCharCode(char);
}
else if ('а'.charCodeAt(0) <= word[i].charCodeAt(0) <= 'я'.charCodeAt(0)) {
let char = ((word[i].charCodeAt(0) + shift - 'а'.charCodeAt(0)) % 32) + 'а'.charCodeAt(0)
result = result + String.fromCharCode(char);
}
else {
result = result + word[i];
}
}
console.log(result)
Change word.legth to word.length
let word = 'привет';
let shift = 3;
let result = '';
for (let i = 0; i < word.length; i++) {
if ('А'.charCodeAt(0) <= word[i].charCodeAt(0) <= 'Я'.charCodeAt(0)) {
let char =
((word[i].charCodeAt(0) + shift - 'А'.charCodeAt(0)) % 32) +
'А'.charCodeAt(0);
result = result + String.fromCharCode(char);
} else if (
'а'.charCodeAt(0) <=
word[i].charCodeAt(0) <=
'я'.charCodeAt(0)
) {
let char =
((word[i].charCodeAt(0) + shift - 'а'.charCodeAt(0)) % 32) +
'а'.charCodeAt(0);
result = result + String.fromCharCode(char);
} else {
result = result + word[i];
}
}
console.log(result);
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())
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++)
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
Here is my code. I am currently stuck in an infinite loop. The program must ask the number of bottles collected for each day for 7 times. So the loop should iterate 7 times. I then must display the total bottles and the total payout of each bottles (which is multiplied but .10) at the end.
function main() {
alert("Welcome to the program");
var totalBottles = 0;
var todayBottles = 0;
var totalPayout = 0;
var keepGoing = "y";
var counter = 1;
while (keepGoing == "y")
getBottles(totalBottles, todayBottles, counter);
calcPayout(totalPayout, totalBottles);
printInfo(totalBottles, totalPayout);
keepGoing = prompt("Do you want to keep going? Enter y for yes");
}
function getBottles(totalBottles, todayBottles, counter) {
while (counter < 7) {
todayBottles = prompt("Enter the number of bottles returned for the day");
totalBottles = (totalBottles + todayBottles);
counter = (counter + 1);
}
}
function calcPayout(totalPayout, totalBottles) {
totalPayout = 0;
totalPayout = (totalBottles * 0.10);
}
function printInfo(totalBottles, totalPayout) {
alert("The total number of bottles returned is", totalBottles);
alert("The total paid out is", totalPayout);
}
//calls main
main();
alert("End of program");
// please try to understand what it's doing and why was your code not printing the correct info.
function main() {
alert("Welcome to the program");
var totalBottles = 0;
var todayBottles = 0;
var totalPayout = 0;
var keepGoing = "y";
var counter = 1;
while (keepGoing == "y") {
totalBottles = getBottles(totalBottles, todayBottles, counter);
totalPayout = calcPayout(totalPayout, totalBottles);
printInfo(totalBottles, totalPayout);
keepGoing = prompt("Do you want to keep going? Enter y for yes");
}
}
function getBottles(totalBottles, todayBottles, counter) {
while (counter < 7) {
todayBottles = prompt("Enter the number of bottles returned for the day");
//alert(todayBottles);
totalBottles += parseInt(todayBottles);
//alert(totalBottles);
counter = (counter + 1);
}
return totalBottles;
}
function calcPayout(totalPayout, totalBottles) {
totalPayout = 0;
totalPayout = (totalBottles * 0.10);
return totalPayout;
}
function printInfo(totalBottles, totalPayout) {
alert("The total number of bottles returned is " + totalBottles);
alert("The total paid out is " + totalPayout);
}
//calls main
main();
alert("End of program");
just put { after
while (keepGoing == "y")
because according to your code your while loop body contains only one line
getBottles(totalBottles,todayBottles,counter);
so your program is stuck on that line as keepGoing is always True
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 8 years ago.
Improve this question
var count=0 ;
for(var x=0; x<data_len; x++)
{
count = count + num_arr[x];
}
// alert(count);
If count = 352 I want to add 3+5+2 which is 10 and then 1+0 which is 1.
function sumParts(x) {
var sumX = 0;
var strX = x.toString();
var arrX = strX.split("");
for (a = 0; a < arrX.length; a++) {
sumX += parseInt(arrX[a], 10);
};
return sumX;
}
y = sumParts(count);
z = sumParts(y);
// y = 10; (3 + 5 + 2)
// z = 1; (1 + 0)
And, I believe (untested), if the return was changed to return sumParts(sumX), it would continue until it was a single digit integer.
You have an array of strings, not numbers. You can convert them to numbers with:
count = count + +num_arr[x];
The second + is the unary plus operator, and will cast num_arr[x] to a number.
If your numbers are all integers, you can use:
count = count + parseInt(num_arr[x], 10);
or (if you have floats):
count = count + parseFloat(num_arr[x]);
Convert count into a string :
var count = 352;
count += ''; // makes a string : "352"
while (count.length > 1) {
count = Function('return ' + count.split('').join('+') + ';')() + '';
}
This part :
Function('return ' + count.split('').join('+') + ';')
Gives successively :
function () { return 3+5+2; }
function () { return 1+0; }