convert string to array of numbers in javascript [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How to convert below String into arrays in javascript
var data = "[
[[1400586475733,-1],
[1400586535736,-1],
[1400586595739,-1],
[1400586655742,-1],
[1400586715745,-1]],
[[1400586475733,0],
[1400586535736,0],
[1400586595739,0],
[1400586655742,0],
[1400586715745,0]]
]";
expected output is
data[0] = [[1400586475733,-1],[1400586535736,-1],[1400586595739,-1],[1400586655742,-1],[1400586715745,-1]]; ==> of type object Array
data[0][0] = 1400586475733 ==> of type number
data[0][1] = -1 ==> of type number

I haven't tried it out thoroughly, but it should work:
var string = "[[[1400586475733,-1],[1400586535736,-1],[1400586595739,-1],[1400586655742,-1],[1400586715745,-1]],[[1400586475733,0],[1400586535736,0],[1400586595739,0],[1400586655742,0],[1400586715745,0]]]";
var dataFromString = string.split(",");
var finalArray = [];
var arrayStartRegEx = new RegExp(/^\[/);
var arrayEndRegEx = new RegExp(/\]$/);
var currentArray = finalArray;
var arrayHistory = [];
for(var i = 0; i < dataFromString.length; i++) {
var currentString = dataFromString[i];
var closingArray = false;
while(arrayStartRegEx.test(currentString) === true) {
// Save previous array
// createArray
// add to big array
// currentArray = createdArray
// remove bracket
var arr = []
arrayHistory.push(currentArray);
currentArray.push(arr);
currentArray = arr;
currentString = currentString.slice(1,currentString.length - 1);
}
while(arrayEndRegEx.test(currentString) === true){
// remove bracket
// add element to array
// close currentArray
// currentArray = big array
currentString = currentString.slice(0,-1);
if(!closingArray) {
var string = currentString.replace(/\]/g, '');
// Use parseFloat if you know it's going to be a float
currentArray.push(parseInt(string));
closingArray = true;
}
currentArray = arrayHistory.pop();
}
if(arrayStartRegEx.test(currentString) === false && arrayEndRegEx.test(currentString) === false) {
// add element to array
currentArray.push(parseInt(currentString))
}
}
console.log(finalArray);

Related

JavaScript: How to create a function that receives an array of numbers and returns an array containing only the positive numbers? [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
What is Wrong with this Code? I should create a function that receives an array of numbers and returns an array containing only the positive numbers. How can it be modified? Especially Modified. Not another code!
all = prompt("Give me an array of numbers seperated by ','");
var splitted = all.split`,`.map(x=>+x);
function returner(splitted){
var positive = [];
for(var i = 0; i < splitted.length; i++);{
var el = splitted[i];
if (el >= 0){
positive.push(el);
}
}
return positive;
}
var positive = returner(splitted);
print(positive);
First I noticed that you are using print to check your output - that should be console.log().
But your real mistake is the semicolon after the for bracket in line 7.
Here is a working code-snippet:
let all = prompt("Give me an array of numbers seperated by ','");
let splitted = all.split`,`.map(x => +x);
function returner(splitted) {
let positive = [];
for (let i = 0; i < splitted.length; i++) {
const el = splitted[i];
if (el >= 0) {
positive.push(el);
}
}
return positive;
}
var positive = returner(splitted);
console.log(positive);
Just remove the semicolon after the for statement as:
all = prompt("Give me an array of numbers seperated by ','");
var splitted = all.split`,`.map(x=>+x);
function returner(splitted){
var positive = [];
for(var i = 0; i < splitted.length; i++){
var el = splitted[i];
if (el >= 0){
positive.push(el);
}
}
return positive;
}
var positive = returner(splitted);
console.log(positive);
practically with that semicolon you were doing "nothing" n times and then executing the block on it's own which didn't help filling your array since the i variable is already passed the last index of the array and so splitted[i] results to undefined which is not >=0 thus nothing gets pushed to the positive array.
(also I'd imagine you want a console.log at the end instead of print? )
Why don't you use filter?
var array = [3, -1, 0, 7, -71, 9, 10, -19];
const getpositiveNumbers = (array) => array.filter(value => value > 0);
var positives = getpositiveNumbers(array);
console.log(positives);
Anyway, as #trincot noticed, your code is wrong.

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);

JAVASCRIPT Can someone fix my code? [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
Can someone help me fix my code?
The function is like the Eval function but has a √ added but it doesn't work
function Eval(n) {
var a = n.split("√").length - 1;
var b = n.split("√").length;
var c = a.replace("√" + d, e);
var d = parseFloat(b[1]);
var e = Math.sqrt(d);
while (a != 0) {
b();
d();
e();
c();
return;
}
}
document.write(Eval("64+√68+32"));
There are some issues with your code and going by your approach, I have updated the code to following. Please see, whether it helps!
Please note, I am assuming as per the name of the function, that you want to evaluate the expression. Also, there are other assumptions as well, like there will be only one square root expression and all operations will be additive.
function Eval(n) {
var b = n.split("√"); // you were expecting b to be an array
var a = b.length - 1; // you can use b here
var d = parseFloat(b[1]); // d should have been assigned before using in c
var e = Math.sqrt(d);
var c = n.replace("√" + d, e);
return c.split("+").reduce(function(a, b) {
return a + parseFloat(b); // sum up all the values
}, 0);
}
console.log(Eval("64+√68+32"));
You can try tweaking here
function eval(n){
var numbers = n.split('+'); // split the exepression with the + operator
var result = 0; // initaliaze the result to 0
for(number of numbers){ // for each number of numbers
if(number.indexOf('√') !== -1){ // if the number contains sqrt
number = +number.substring(1, number.length); // cast to int
result += Math.sqrt(number);
}else{
number = +number; // cast string to int, NaN if not possible
result += number;
}
}
return result;
}
This function will work for you additions.
Not that this is just a point to start, and not the best way of doing it, I tried to be the more comprehensive seeing that you are a beginner in javascript

Spaces in a joined array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have been working on a ceaser cipher algorithm but I haven't been able to grasp the reason why the joined array returns spaces in a peculiar state.
function rot13(str) { // LBH QVQ VG!
var string = str.split('');
var codedStr = [];
var encoded = [];
for (var k=0; k < string.length; k++){
codedStr.push(string[k].charCodeAt());
}
for(var i = 0; i < codedStr.length; i++){
if(codedStr[i] > 77 ){
codedStr[i] -= 13;
}
else if( codedStr[i] == 32 || codedStr[i] == 63){
codedStr[i] = codedStr[i];
}
else{
codedStr[i] += 13;
}
encoded.push(codedStr[i]);
}
var decode = codedStr.map(String.fromCharCode);
var result = decode.join('');
return result;
}
// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC"));
String.fromCharCode accepts multiple arguments, and map provides 3. You should use
codedStr.map(code => String.fromCharCode(code));

Categories

Resources