Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How to loop on a number using jQuery from 1 to 10.
Let's say I have
var start=8;
var end=13;
Expected Output:
8 9 10 1 2 3
On reaching 10, the output series restarts from 1.
Try this
var start = 8, end = 13;
for(var i = start; i <= end; i++){
var num = i > 10 ? i % 10 : i;
console.log(num);
}
Try this
var start = 8;
var end = 13;
for(var i = start; i <= end; i++){
var result = i % 10;
if(result == 0){
result = 10;
}
console.log(result);
}
Try this it is quite straightforward.
var start = 8, end = 13;
for(var i = start; i <= end; i++){
console.log(i % 10 === 0 ? 10: i % 10);
}
var start = 5,
end = 13;
var i = start;
var j = start;
while (j >= start && j < end) {
console.log(i);
i = (i + 1) % 10;
j++;
}
Related
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 1 year ago.
Improve this question
I've got trouble with arrays. I'm trying to iterate elements for each position in the array using javascript.
so, I need to do that on the first position, be a value [1]. On the second position, [2, 3, 4]. on the third position, [5,6,7,8,9] and successively two by two.
My attempt was did a for loop:
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= 5; j++) {
console.log(`${i} ${j}`)
}
}
But while looping, the indices (i) repeat with (j).
What to do?
Try this code.
var counting = 1;
for (let i = 1; i <= 5; i = i + 2) {
var str = "";
for (let j = 1; j <= i; j++) {
str += `${counting} `;
counting++;
}
console.log(str);
}
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 1 year ago.
Improve this question
i want to create 2 of these pyramid evenly i have tried adding another else if but it just override it instead
var str5 = "";
for (let i=1; i < 10; i++) {
for (let j=1; j < 10; j++) {
if (i<= 5 && j >= (5) - (i - 1) && j <= (5) + (i - 1)){
str5 = str5.concat("*")}else if ( i > 5 && j <= (5 - (5 - i)*(-1)) && j >= (i - 4)){
str5 = str5.concat("*")
} else {
str5 = str5.concat(" ")
}
}
str5 = str5.concat("\n")
}
console.log(str5);
Here's a more generic way to do it:
With a base of 5, first row will have all stars; second row: blanks replace stars in first and last spots; third row: blanks replace stars in first, second, second last and last spots
So, for any odd base, each row progressively has blanks replace stars from outside in until there is a single star
To duplicate horizontally, just repeat the process n times per row
In this snippet, the symbolPos array keeps track of which 'spots' you want the symbol and blank otherwise. symbolPos has its leading and trailing element removed for each row iteration.
function invertedPyramidString(base, repeats, symbol) {
let str = "";
if (base % 2 ===0) return "base argument must be odd";
let rows = (base + 1) / 2; // rows should be half of (base + 1)
let symbolPos = [...Array(base).keys()]; // [0, 1, 2... base-1]
for (let row=0; row < rows; row++) {
for (let rep=0; rep < repeats; rep++) {
for (let col=0; col<base; col++) {
str = str.concat(symbolPos.indexOf(col) > -1 ? symbol : " ");
}
}
symbolPos.shift(); // remove 1st index
symbolPos.pop(); // remove last symbol
str = str.concat("\n");
}
return str;
}
console.log("Base 5; repeats 2; symbol *");
console.log(invertedPyramidString(5, 2, "*"));
console.log("");
console.log("Base 5; repeats 4; symbol *");
console.log(invertedPyramidString(5, 4, "*"));
console.log("");
console.log("Base 7; repeats 3; symbol ^");
console.log(invertedPyramidString(7, 3, "^"));
console.log("");
console.log("Base 13; repeats 4; symbol !");
console.log(invertedPyramidString(13, 4, "!"));
console.log("");
console.log("Base 4; repeats 2; symbol *");
console.log(invertedPyramidString(4, 2, "*"));
console.log("");
.as-console-wrapper { max-height: 100% !important; top: 0; }
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 2 years ago.
Improve this question
In response I want all 3 digit numbers whose sum is 11 and also gives module 1 and
also want to know how can I push 000 in array in js.
For example number = 128, sum = 11 and module(remainder) = 1.
Like this I want all that numbers in array.
Can anyone share the logic for the same.
Thanx in advance.
I'm not sure what the second check is supposed to be (the modulus of something has to be 1). If that is supposed to be that the modulus of all digits of the number has to be 1, here is how you could do it:
var results = [];
for (let i = 1; i < 10; i++) {
for (let j = 0; j < 10; j++) {
for (let k = 0; k < 10; k++) {
if (i + j + k === 11 && i % j % k === 1) {
results.push(i.toString() + j.toString() + k.toString());
}
}
}
}
console.log(results);
OR:
var results = [];
for (let i = 100; i < 1000; i++) {
const [one, two, three] = i.toString().split('').map(i => +i);
if (one + two + three === 11 && one % two % three === 1) {
results.push(i.toString());
}
}
console.log(results);
Basically just go through all the possible combination and do the checks on each number.
Is this what you want?
var arr=[1,128,15];
var remainder = 127;
var result = arr.filter(function(i) {
var str=i.toString().split('');
var sum=0;
str.forEach(function(c) { return sum+=Number(c); });
return sum===11 && (i%remainder)===1;
});
and more fancy and ES6:
const arr=[1,128,15];
const remainder = 127;
const result = arr.filter(i=>{
const sum=i.toString().split('').reduce((s,v)=>(+v)+s,0);
return sum===11 && (i%remainder)===1;
});
You could take a brute force approach and check the sum with eleven. A check with the remainder is not necessary, because 11 has always a rest of one, if divided by ten.
const
getSum = v => [...v.toString()].reduce((a, b) => a + +b, 0),
values = [];
for (let i = 1; i < 1000; i++) {
if (getSum(i) === 11) values.push(i);
}
console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }
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 2 years ago.
Improve this question
Can anyone help me with this Javascript problem? The code I've write is not quite right.
function tes() {
var c = document.getElementById('Text');
var a = document.getElementById('a').value;
var txt = '';
for (i = 1; i <= a; i++) {
for (j = 1; j <= a; j++) {
if (j == i) {
txt += i.toString();
} else {
txt += i.toString();
}
}
txt += '<br/>';
}
c.innerHTML = txt;
}
<input id="a">
<button onclick="tes()">Input</button>
<div id="Text"></div>
Javasript Problem
Expected output:
1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5
Since your problem required an input, you would be running the code when input is received. The only thing you got incorrect was really the if statement logic. rowAmm is a variable holding for the amount of 1s in a row, if j was less than that, add another 1, otherwise add i which was the row number.
You won't have to do a .toString() function either if you're adding it to a variable which is already declared a string var txt = ''. Just like 5 + "5" will become 55 while 5 + 5 will be 10
var a = document.getElementById("a");
var textBox = document.getElementById("Text");
function makeSquare() {
var b = a.value;
var txt = '';
for (i = 1; i <= b; i++) {
var rowAmm = b - i;
for (j = 1; j <= b; j++) {
if (j <= rowAmm) {
txt += "1";
} else {
txt += i;
}
}
txt += '<br/>';
}
textBox.innerHTML = txt;
}
<input id="a">
<button onclick="makeSquare()">Input</button>
<div id="Text"></div>
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 6 years ago.
Improve this question
so this is what I've achieved so far
function arrayMode(sequence) {
var arr= [];
var mostFreq = 1;
for(var i = 1; i <= 10; i++)
arr[i] = 0;
for(var i = 0 ; i < sequence.length; i++)
arr[sequence[i]]++;
for(var i = 1; i < 10; i++){
if( arr[i] > arr[mostFreq])
mostFreq = i;
}
return mostFreq;
}
so the scope for my case are
The sequence value is >= 1 and <=10
The sequence length is > 0
The sequence is an array of integer
Example 1
Input : sequence = [1, 10, 10]
Output : 10
Example 2
Input : sequence = [1, 3, 3, 3, 1]
Output : 3
It seems easy, I've tried to figure out but I cant find where is the mistake in my code, It's seems legit for me
As suggested by Maxim Krizhanovsky, the last loop should go from 1 to 10 instead of to sequence.length. Here is the working code
function arrayMode(sequence) {
var arr= [];
var mostFreq = 1;
for(var i = 0; i <= 10; i++)
arr.push(0);
for(var i = 0 ; i < sequence.length; i++)
arr[sequence[i]]++;
for(var i = 1; i <= 10; i++){
if( arr[i] > arr[mostFreq])
mostFreq = i;
}
return mostFreq;
}
The output is 10 and 3 respectively to your input.