simple javascript number game [closed] - javascript

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 months ago.
Improve this question
substract 7 every time (starting number is 100), the first answer will be 93, i dont want user to move on until he gets the first answer right
let answerInput = document.getElementById("inpu");
let checkButton = document.getElementById("send");
let answers = [];
for(let i = 100; i>2;){
i -= 7;
answers.push(i);// [93, 86,...]
}
checkButton.onclick = function(){
//my struggle here
if(isNaN(answerInput.value) === true){
answerInput.value = "";
answerInput.placeholder = "only numbers are allowed";
}
}

There is no need to store the numbers in an array, seen as the next number is always 7 from the last, just keep a counter for the number. And then just compare this to the input - 7;
eg.
const span = document.querySelector('span');
const input = document.querySelector('input');
const button = document.querySelector('button');
let number = 100;
const showNum = () => span.innerText = number;
showNum();
button.addEventListener('click', () => {
const want = number - 7;
if (Number(input.value) === want) {
if (want == 2) {
alert("Well done you..");
} else {
number -= 7;
showNum();
}
}
});
Subtract 7 from <span></span> = <input type="number" value="100"/><button>Try</button>

Solution:
let answerInput = document.getElementById("inpu");
let checkButton = document.getElementById("send");
let answers = [];
for (let i = 100; i > 2; ) {
i -= 7;
answers.push(i); // [93, 86,...]
}
checkButton.onclick = function () {
if (isNaN(answerInput.value) === true) {
answer.value = "";
answer.placeholder = "only numbers are allowed";
}
// check if answer correct, `!=` compare with type coercion
else if (answerInput.value != answers[0]) {
answer.placeholder = "try again"
}
else {
answers.shift(); // remove first array element
answer.placeholder = "correct!"
}
};
<!DOCTYPE html>
<html>
<head></head>
<body>
<!--
You can use type="number" to auto-avoid letters in field:
<input id=inpu type="number" />
-->
<input id=inpu />
<input disabled id=answer />
<button id=send>Send</button>
</body>
</html>

Related

How can we change heading with respect to time using javascript? [closed]

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 2 years ago.
Improve this question
I want to change the heading of the web page. Like I have
<h1>hey there!</h1>
so want to change this heading text with another text with respective time.
and changes should be in the loop.
like I used my values to be store in an array.
eg. var values = [ 'hi', 'hello', 'bye']
how can I use the set.timeout function in for loop to change those values continuously.
You can try like this.
let targetElement = document.querySelector('h1');
var values = ['hi', 'hello', 'bye'];
var totalArraylength = values.length;
var counter = 0;
setInterval(() => {
if(counter<totalArraylength){
targetElement.innerHTML = values[counter];
counter += 1;
}else{
counter = 0
}
}, 1000);
<h1>hey there!</h1>
Title can be changed by using the title tag. If you wanna count how many times for the loop, you can implement it by taking a look on the above answers.
const title = document.querySelector('title')
const values = [ 'hi', 'hello', 'bye']
values.forEach((elem) => {
setInterval(() => {
title.innerText = elem
}, 2000)
})
In a simple manner with DOM you can do it like this way
var count = 0;
var titleList = ['hi', 'hello', 'bye'];
var intervalsFunc = setInterval(() => {
if ( count >= (titleList.length - 1)) {
clearInterval(intervalsFunc);
}
document.getElementsByTagName("h1")[0].innerHTML = titleList[count] ;
count++
},
1000);
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript can Change HTML</h1>
</body>
</html>
let values = ['My', 'array', 'values'];
let currentIndex = 0;
let changeValue = () => {
let node = document.querySelector('h1');
node.innerText = values[currentIndex];
if (currentIndex < values.length + 1) {
setTimeout(() => {
currentIndex++;
changeValue();
}, 1000);
}
}
changeValue()
<h1>hey there!</h1>

my javascript code is not linking to the dom [duplicate]

This question already has answers here:
What is innerHTML on input elements?
(10 answers)
Closed 2 years ago.
I was challenged to create a function that recognizes a prime number and that was working great on the console until I linked my const number to the input and my function to the button.
const btn = document.querySelector('.btn1')
if (btn) {
btn.addEventListener('click', isitPrime)
}
function isitPrime() {
const number = document.querySelector('.input1').innerHTML
const answer = document.querySelector('.answer')
let isPrime = true
for (let i = 2; i < number; i++) {
if (number % i === 0) {
isPrime = false
answer.innerHTML = (`${number} is not prime
${number} can be divided by ${i}`)
}
if (isPrime) {
answer.innerHTML = (`${number} is prime`)
}
}
}
<input type="text" class="input1">
<button class="btn1">click here</button> <br/><br/>
<div class="answer"></div>
How to solve this problem?
In order to get the value of an input you have to use the value property instead of the innerHtml one, like this :
const btn = document.querySelector('.btn1')
if(btn){
btn.addEventListener('click', isitPrime)
}
function isitPrime(){
// here we get 'value' property instead of 'innerHtml' property
const number = document.querySelector('.input1').value
const answer = document.querySelector('.answer')
let isPrime = true
for(let i = 2; i < number; i++){
if(number % i === 0){
isPrime = false
answer.innerHTML = (`${number} is not prime
${number} can be divided by ${i}`)
}
if(isPrime){
answer.innerHTML = (`${number} is prime`)
}
}
}
<input type="text" class="input1">
<button class="btn1">click here</button> <br/><br/>
<div class="answer"></div>
document.querySelector('.input1').innerHTML
This is wrong
You should use
document.querySelector('.input1').value

Javascript: Find the second longest substring from the given string Ex: I/p: Aabbbccgggg o/p: bbb [closed]

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
Javascript:Find the second longest substring from given string(input and output example added in heading)
Try this
Get sequences using RegExp
Sort them based on string length
Select second Item
function getSecondSubstring(str){
let regex = new RegExp(str.toLowerCase().split("").filter((x,i,a)=>a.indexOf(x)===i).join("+|")+"+", "ig")
let substrgroups = str.match(regex);
substrgroups.sort((a,b)=> b.length-a.length);
return substrgroups[1]
}
console.log(getSecondSubstring("ööööööðððób"));
console.log(getSecondSubstring("Aabbbccgggg"));
If you don't mind using regular expressions:
function yourFunctionName(input){
let grp = input.split(/(?<=(.))(?!\1|$)/ig);
grp.sort((a,b)=> b.length-a.length);
if(grp.length <= 0){
return null;
}
else if (grp.length == 1){
return grp[0];
}
else{
grp.sort(function(a, b){
return b.length - a.length;
});
return grp[1];
}
}
console.log(yourFunctionName("ööööööðððób"));
Or another way which does not use regular expressions...
function yourFunctionName(input){
input = input.toLowerCase();
let counter = [];
let prevChar;
let countIndex = 0;
for (let index = 0, length = input.length; index < length; index++) {
const element = input[index];
if(prevChar){
if(prevChar != element){
countIndex++;
counter[countIndex] = "";
}
}
else{
counter[countIndex] = "";
}
counter[countIndex] += element;
prevChar = element;
}
if(counter.length <= 0){
return null;
}
else if (counter.length == 1){
return counter[0];
}
else{
counter.sort(function(a, b){
return b.length - a.length;
});
return counter[1];
}
}
console.log(yourFunctionName("aaaaabbbbccdd"));

Check if two objects with different structure are identical? [closed]

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 trying to check if two objects are identical, using Pid and Vid.. I know how to do it in PHP, but JavaScript is so hard for me...
// Object 1
{"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}}
// Object 2 (Pid:Vid,Pid:Vid,Pid:Vid,Pid:Vid)
{"1627207":"38330499","10004":"513661344","20122":"103646","5919063":"6536025"}
EDIT:
I did it this way, but it's really slow...
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var details = JSON.parse('{"ConfiguredItems":{"OtapiConfiguredItem":[{"Id":"3657280986465","Quantity":"8674","Price":{"OriginalPrice":"839.00","MarginPrice":"839","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"839","DisplayedMoneys":{"Money":"128.84"}},"ConvertedPrice":"128.84$","ConvertedPriceWithoutSign":"128.84","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"839.00","MarginPrice":"839","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"839","DisplayedMoneys":{"Money":"128.84"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}},{"Id":"3657280986466","Quantity":"9878","Price":{"OriginalPrice":"869.00","MarginPrice":"869","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"869","DisplayedMoneys":{"Money":"133.45"}},"ConvertedPrice":"133.45$","ConvertedPriceWithoutSign":"133.45","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"869.00","MarginPrice":"869","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"869","DisplayedMoneys":{"Money":"133.45"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"3266779"}}]}},{"Id":"3657280986467","Quantity":"9989","Price":{"OriginalPrice":"889.00","MarginPrice":"889","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"889","DisplayedMoneys":{"Money":"136.52"}},"ConvertedPrice":"136.52$","ConvertedPriceWithoutSign":"136.52","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"889.00","MarginPrice":"889","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"889","DisplayedMoneys":{"Money":"136.52"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"3266781"}}]}},{"Id":"3657280986468","Quantity":"9995","Price":{"OriginalPrice":"919.00","MarginPrice":"919","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"919","DisplayedMoneys":{"Money":"141.12"}},"ConvertedPrice":"141.12$","ConvertedPriceWithoutSign":"141.12","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"919.00","MarginPrice":"919","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"919","DisplayedMoneys":{"Money":"141.12"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"3266785"}}]}},{"Id":"3657280986469","Quantity":"9994","Price":{"OriginalPrice":"959.00","MarginPrice":"959","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"959","DisplayedMoneys":{"Money":"147.27"}},"ConvertedPrice":"147.27$","ConvertedPriceWithoutSign":"147.27","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"959.00","MarginPrice":"959","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"959","DisplayedMoneys":{"Money":"147.27"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"3266786"}}]}},{"Id":"3657280986470","Quantity":"8993","Price":{"OriginalPrice":"869.00","MarginPrice":"869","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"869","DisplayedMoneys":{"Money":"133.45"}},"ConvertedPrice":"133.45$","ConvertedPriceWithoutSign":"133.45","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"869.00","MarginPrice":"869","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"869","DisplayedMoneys":{"Money":"133.45"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}},{"Id":"3657280986471","Quantity":"9687","Price":{"OriginalPrice":"899.00","MarginPrice":"899","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"899","DisplayedMoneys":{"Money":"138.05"}},"ConvertedPrice":"138.05$","ConvertedPriceWithoutSign":"138.05","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"899.00","MarginPrice":"899","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"899","DisplayedMoneys":{"Money":"138.05"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"3266779"}}]}},{"Id":"3657280986472","Quantity":"9932","Price":{"OriginalPrice":"919.00","MarginPrice":"919","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"919","DisplayedMoneys":{"Money":"141.12"}},"ConvertedPrice":"141.12$","ConvertedPriceWithoutSign":"141.12","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"919.00","MarginPrice":"919","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"919","DisplayedMoneys":{"Money":"141.12"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"3266781"}}]}},{"Id":"3657280986473","Quantity":"9959","Price":{"OriginalPrice":"949.00","MarginPrice":"949","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"949","DisplayedMoneys":{"Money":"145.73"}},"ConvertedPrice":"145.73$","ConvertedPriceWithoutSign":"145.73","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"949.00","MarginPrice":"949","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"949","DisplayedMoneys":{"Money":"145.73"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"3266785"}}]}},{"Id":"3657280986474","Quantity":"9965","Price":{"OriginalPrice":"989.00","MarginPrice":"989","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"989","DisplayedMoneys":{"Money":"151.87"}},"ConvertedPrice":"151.87$","ConvertedPriceWithoutSign":"151.87","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"989.00","MarginPrice":"989","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"989","DisplayedMoneys":{"Money":"151.87"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"3266786"}}]}},{"Id":"3657280986475","Quantity":"9409","Price":{"OriginalPrice":"949.00","MarginPrice":"949","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"949","DisplayedMoneys":{"Money":"145.73"}},"ConvertedPrice":"145.73$","ConvertedPriceWithoutSign":"145.73","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"949.00","MarginPrice":"949","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"949","DisplayedMoneys":{"Money":"145.73"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"6630567"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}},{"Id":"3657280986476","Quantity":"9661","Price":{"OriginalPrice":"979.00","MarginPrice":"979","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"979","DisplayedMoneys":{"Money":"150.34"}},"ConvertedPrice":"150.34$","ConvertedPriceWithoutSign":"150.34","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"979.00","MarginPrice":"979","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"979","DisplayedMoneys":{"Money":"150.34"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"6630567"}},{"#attributes":{"Pid":"5919063","Vid":"3266779"}}]}},{"Id":"3657280986477","Quantity":"9911","Price":{"OriginalPrice":"999.00","MarginPrice":"999","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"999","DisplayedMoneys":{"Money":"153.41"}},"ConvertedPrice":"153.41$","ConvertedPriceWithoutSign":"153.41","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"999.00","MarginPrice":"999","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"999","DisplayedMoneys":{"Money":"153.41"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"6630567"}},{"#attributes":{"Pid":"5919063","Vid":"3266781"}}]}},{"Id":"3657280986478","Quantity":"9954","Price":{"OriginalPrice":"1029.00","MarginPrice":"1029","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"1029","DisplayedMoneys":{"Money":"158.02"}},"ConvertedPrice":"158.02$","ConvertedPriceWithoutSign":"158.02","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"1029.00","MarginPrice":"1029","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"1029","DisplayedMoneys":{"Money":"158.02"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"6630567"}},{"#attributes":{"Pid":"5919063","Vid":"3266785"}}]}}]}}');
var configs = [];
configs[10004] = "513661344";
configs[20122] = "103646";
configs[1627207] = "38330499";
configs[5919063] = "6536025";
function updateConfig(pid, vid) {
var id = null;
for(var i = 0; i < details.ConfiguredItems.OtapiConfiguredItem.length; i++) {
var OtapiConfiguredObj = details.ConfiguredItems.OtapiConfiguredItem[i];
var current = [];
for(var j = 0; j < OtapiConfiguredObj.Configurators.ValuedConfigurator.length; j++) {
var ValuedConfiguratorObj = OtapiConfiguredObj.Configurators.ValuedConfigurator[j];
current[ValuedConfiguratorObj['#attributes'].Pid] = ValuedConfiguratorObj['#attributes'].Vid;
}
if(JSON.stringify(current) === JSON.stringify(configs)) {
id = OtapiConfiguredObj.Id;
break;
}
}
console.log(id); // Display result ID
}
updateConfig();
</script>
</head>
</html>
Converting obj2 to a Map and iterating over the ValuedConfigurator using .every() makes it pretty simple.
// Object 1
const obj1 = {"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}}
// Object 2 (Pid:Vid,Pid:Vid,Pid:Vid,Pid:Vid)
const obj2 = {"1627207":"38330499","10004":"513661344","20122":"103646","5919063":"6536025"}
const vc = obj1.Configurators.ValuedConfigurator;
const m = new Map(Object.entries(obj2));
const areEqual = vc.length == m.size &&
vc.every(({"#attributes":{Pid, Vid}}) => m.get(Pid) === Vid);
console.log(areEqual);

User inputs the number of unique numbers they want to generate in javascript [duplicate]

This question already has answers here:
Random number generator without dupes in Javascript?
(7 answers)
array of random numbers [duplicate]
(1 answer)
Closed 8 years ago.
I am looking to generate x number of unique numbers between 1-10 ( whatever the user types in a input box) . So far I can generate x number of the same unique number. It needs to have an input.
function addFields(){
var number = document.getElementById("member").value;
var container = document.getElementById("container");
var arr = Math.floor((Math.random() * 10) + 1);
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (i=0;i<number;i++){
container.appendChild(document.createTextNode(" " + (arr)));
var input = document.createElement("input");
input.type = "number";
container.appendChild(document.createElement("br"));
}
}
https://jsfiddle.net/andrew1814/by62764z/1/
Thanks for your help!
You want to generate everytime a random number so you can move arr variable inside the iterator:
function addFields() {
var number = document.getElementById("member").value;
var container = document.getElementById("container");
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (i = 0; i < number; i++) {
//move arr here
var arr = Math.floor((Math.random() * 10) + 1);
container.appendChild(document.createTextNode(" " + (arr)));
var input = document.createElement("input");
input.type = "number";
container.appendChild(document.createElement("br"));
}
}
<input type="text" id="member" name="member" value="">Number of members: (max. 10)
<br />
See Numbers
<div id="container" /></div>
Here is a function that will generate an array of x unique numbers 1-10:
function generateUnique (x) {
var numbers = [];
while(x > 0) {
var r = Math.floor((Math.random() * 10) + 1);
if(numbers.indexOf(r) === -1) {
numbers.push(r);
x--;
}
}
return numbers;
}
alert(generateUnique(5));

Categories

Resources