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 5 days ago.
Improve this question
I have created an Array of objects.
let products = [
{
productName: 'product1';
},
{
productName: 'product2';
},
{
productName: 'product3';
},
{
productName: 'product4';
},
{
productName: 'product5';
},
]
I have looped through this Array and displayed all of the elements using the following code:
for (let i of products) {
//Create Card
let card = document.createElement("div");
//product name
let name = document.createElement("h5");
name.innerText = i.productName.toUpperCase();
container.appendChild(name);
card.appendChild(container);
document.getElementById("products").appendChild(card);
}
I have created buttons for this Array. There should be 20 objects per page. I have created the buttons using this code:
let first_product = 0;
let second_product = 20;
let page_number = 1;
let product_number = 1;
for (let i of products) {
//Create Card
product_number += 1;
if (product_number % 20 === 0) {
let button = document.createElement('button');
button.classList.add('button-value');
button.innerHTML = page_number;
page_number += 1;
document.getElementById('nav-buttons').appendChild(button);
first_product += 20;
second_product += 20;
}
}
This code works correctly. I now want to add the cards to each page. If the user clicks on Page 1, the first 20 cards should be shown. If the user clicks on page 2, the next 20 cards should be shown.
I have looked online and have not been able to find an answer. I have also looked at multiple questions on Stack Overflow. However, I have not been able to find a correct answer.
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 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
im trying to build small project .
<body>
<button class="btnCheck">Click Me</button>
<p class"randomNumber">Random Number:</p>
<p class"recentNumbers">Last Numbers:</p>
<script>
let randomNumber = Math.floor(Math.random() * 20 + 1)
const recentNumbers = document.querySelector(".recentNumbers");
const btnCheck = doucment.querySelector(".btnCheck");
let paraNumber = doucment.querySelector(".randomNumber");
btnCheck.addEventListener("click",function(){
paraNumber.textContent = randomNumber;
for(let i = 0; i < randomNumber.textContent[i]; i++){
recentNumbers.textContent = randomNumber.textContet[i]'
})
</script>
// 2nd method :
randomArr = [];
recentNumbers.textContent = randomArr.push(randomNumber);
if (randomArr.length >= 5) {
randomArr.shift()
}
</script>
</body>
What I have been trying to do is : to show the last random numbers to limit to 4 recent numbers, everytime i click in the button,
when i pushed it to array it is not working and also it is creating new array everytime im clicking the button in the 2nd method
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
[Ask]
anyone know the reference for a case like this?
the user must type the word: "learn" -> total character 5
while the user types instead: "l3arn"
well I want to count the right word only, so the result 4
from google not yet find a solution, maybe someone knows the keyword/reference for the problem.
I want to implement it in javascript
You want to calculate the number of characters in the correct position?
In that case, it's a simple solution.
Javascript example:
function countCorrectCharacters(expectedString, string) {
var count = 0;
var l = Math.min(expectedString.length, string.length);
for (var i = 0; i < l; ++i) {
if (expectedString[i] === string[i]) {
++count;
}
}
return count;
}
var str = "learn";
var userInput = "l3arn";
var userInputArray = userInput.split('');
var counter = 0;
for(var i = 0; i< "l3arn".lenth(); i++){
if(str.indexOf(userInputArray[i]) !== -1) counter++;
}
If I understand correctly you want to count the number of letters in a string? Use this:
function getNumberOfLetters(string) {
let match = string.match(/[A-Za-z]/g);
return (match && match.length) || 0;
}
console.log(getNumberOfLetters("learn")); // 5
console.log(getNumberOfLetters("l3arn")); // 4
console.log(getNumberOfLetters("12345")); // 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 5 years ago.
Improve this question
I'm afraid it might be a silly question, but I have no idea what occurs my problem.
I dynamically create buttons (each button has unique id), and I store each btn reference (document.getElementById()) in simple two-dimensional array. All these because i need to hold btns in well organized structure.
The problem is: when i try to modify element by reference previously stored in my array, there appears to be no connection between ref and html element.
var Table = {
size: 10,
table: [],
generateTable: function() {
var i = 0,
j = 0,
id = null,
tablePlaceholder = document.getElementById("tableHTML");
//Generate table
for (i = 0; i < this.size; i++) {
this.table.push([]);
for (j = 0; j < this.size; j++) {
id = i.toString() + "-" + j.toString();
tablePlaceholder.innerHTML += element(id);
this.table[i].push(document.getElementById(id));
}
tablePlaceholder.innerHTML += "</br>";
}
console.log(this.table[0][0].className);
document.getElementById("0-0").className += " btn-success";
console.log(this.table[0][0].className);
}, ...
Results of last two console.logs is the same, but element has changed in DOM.
table[0][0] returns same content as document.getElementById("0-0").
(element() function returns simple html code (it works well))
innerHTML += '<div>...</div>';
This could break references to already constructed DOM elements and cause other chaos. In reality, all you want to do is append a single new element to the end.
var elem = document.createElement('div');
elem.id = id;
tablePlaceholder.appendChild(elem);
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 5 years ago.
Improve this question
var sunny=[1,2,3];
var bunny=[4,5,6];
var name=prompt("Enter Name");
for(var i=0;i<3;i++)
{
document.write(name[i]);
}
//If User input Sunny then array elements of sunny will be printed.
You can use object for users and write your users in it, example:
var users = {
sunny: [1,2,3],
bunny: [4,5,6]
}
var name = prompt("Enter Name");
console.log(users[name]);
//If User input Sunny then array elements of sunny will be printed.
I think what you want is to use an object:
const names = {
sunny:[1,2,3],
bunny:[4,5,6],
};
const name=prompt("Enter Name");
for(var i=0;i<3;i++)
{
document.write(names[name][i]);
}
There is not clean way to get a variable if you have its name as string. However its easy to access an objects properties with a string with the [] syntax.
U can achieve this by using 'eval'.
See the update code.
var sunny=[1,2,3];
var bunny=[4,5,6];
var name=prompt("Enter Name");
for(var i=0;i<3;i++)
{
document.write(eval(name)[i]);
}
You can use a switch-statement (https://www.w3schools.com/js/js_switch.asp) like
var a;
switch(name) {
case "sunny":
a = sunny;
break;
case "bunny":
a = bunny;
break;
}
for(var i = 0; i < a.length; i++) {
document.write(a[i]);
}
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 8 years ago.
Improve this question
I have the following Javascript code:
var Lab = {
minseat: function(){
var seat = 10;
return seat;
}
maxseat: function(){
var seat = 50;
return seat;
}
}
So, when I need to get the value, I simply call the following code:
console.log(" Min Seat: " + Lab.minseat());
console.log(" Max Seat: " + Lab.maxseat());
However, it seems it is not working. So, may I know if it is possible to have a Javascript variable to have two or more functions inside?
You forgot a comma after the first function:
var Lab = {
minseat: function(){
var seat = 10;
return seat;
},
maxseat: function(){
var seat = 50;
return seat;
}
}
How about a comma separating the keys in your object definition.
var Lab = {
minseat: function(){
var seat = 10;
return seat;
},
maxseat: function(){
var seat = 50;
return seat;
}
}
http://jsfiddle.net/V4Yje/