Jump to content

smus

Members
  • Content Count

    134
  • Joined

  • Last visited

Everything posted by smus

  1. smus

    JSON and object

    If I have a JSON data, can I apply to it as if it is a JS Object, or I should use JSON.parse() first? Will it skip the property names quotes? ("name":"value" or name:"value") If there are some associative arrays inside and the structure is complex, is it easier to use array.map() method or it is better to apply to the properties directly?
  2. smus

    JSON and object

    Thanks
  3. smus

    JSON and object

    No other way? No kinda search method for big and complex objects?
  4. smus

    JSON and object

    Why is map() method often used for these reasons? Might that be because the structure is complex and I know the name of the property and I am not sure about its hierarchical disposition? For example, a = { first : ['propertyOne' = 'po', 'propertyTwo' = 'pt'], second : [smallObj = {s1:'s1', s2: 's2', s3: 's3'}, smallObj2 = {s1:'s1'}, ['a1', 'a2']], third : ['t1', [{'whatIWantToFind':'someInfo'}], t3, 'abcdef'] } I have to find the 'whatIWantToFind' property inside a object. I know it is inside the object, but I don't know where exactly it is. Do I need to use a map() or any other method for it?
  5. smus

    equation solver

    How do I solve the equation: 210x+65y=5 x and y must be integers
  6. smus

    equation solver

    It's a part of a more complex task. 5 is already a result, it is a GCD (greatest common divisor) of those 2 integers (210,65). They are function input parameters, but now it has to return x and y, not only GCD.
  7. smus

    equation solver

    yes, it's needed to be solved by js
  8. smus

    Code optimization

    How can I optimize this code? It's correct, but performance tests scored just 20%: function solution(A){ var al = A.length, c = 0; for(var x=0;x<al;x++){ if(A[x] == 0){ for(var y=x;y<al;y++){ if(A[y] == 1){ c++;} } } } return c; } console.info(solution([0,1,0,1,0,1,0])); //6 Here is the link to the task: https://app.codility.com/demo/results/trainingM8PUJV-HEU/
  9. smus

    Code optimization

    Where n is the amount of nested loops? According to your explanation, it is better to have several independent loops, than one nested loop: for(){} for(){} for(){} for(){} would be faster than: for(){ for(){} }
  10. smus

    Array into a formula

    Elements of an array of a math formula look like this: a = [1,'+',2,'-',3,'*',4,'/',5]; How do I perform all the arythmetic actions between those numbers. The array elements may be different, because they are added dynamically. It would not be difficult if I didn't have to prioritize the actions (/,*,_,-). I tried using splice() method, but something goes wrong. Maybe I can somehow sort the elements according to the math actions priority?
  11. smus

    Array into a formula

    Exactly, right. The precedence goes pair(* and /) over pair(+ and -) and the Win Calc must be set to a kind of formula mode to use precedence order, otherwise it counts numbers from left to right as they are. Thanks!
  12. smus

    Array into a formula

    eval() works in this case, but I am not sure what the right priority is (operator precedence) in Javascript. Some websites has info that divistion(/) has the highest priority then goes multiplication, addition and subtraction. That is strange, because usually multiplication is the first. 1-2+3*4/5 = 1.4 according to JS compliler (/ then * then - then +, no idea why it counts like that) 1-2+3*4/5 = 1.6 according to Windows Calc (I guess, it counts without any priority) 1-2+3*4/5 = -3.4 if we use the correct operator precedence (* / + -) What I was taught in high school is that the correct precedence must be: * / + - Never thought that there might be another order in programming languages.
  13. smus

    Code optimization

    Thanks, perfect! So, what I can learn from it, including additional loops significantly increases the script's implementation time. Am I right?
  14. smus

    Code optimization

    It scored 70% Passed only one more of performance tests (large_extreme: large test with all 1s/0s, length = ~100,000, approx 0.100s) https://app.codility.com/demo/results/training95UXWY-GN8/
  15. smus

    Array declaration

    I would like to create an array with 2 elements, for example. When I assign the length explicitly, it works well, but this code does not work: dim n=2 dim a(n) a(0) = "first" a(1) = "second" response.write a(0)&a(1) how to set array length dynamically?
  16. smus

    Array declaration

    a() redim a(n) now it works, thanks
  17. smus

    Creating Dynamic Checkbox Names

    if pDiscussed is of boolean type, you don't need quotes around the true value. Try checked = "checked" as well What is stored in i variable? Is that (pDiscussed(i)) an array element?
  18. smus

    Universal selector

    Is it possible to apply to several tags with one selector, not creating class for it? For example, I want a common property for all headings in the document: h1, h2, h3{color: orange;} <h1>h1</h1> <h2>h2</h2> <h3>h3</h3> Something like h^{color: orange;} for tag selectors?
  19. smus

    how make many id in one getElementById

    same thing in javascript: var test = document.getElementsByClassName("test"); for (var i=0;i<test.length;i++){ test[i].value = Dotoff; }
  20. smus

    Universal selector

    naive solution, because, hypothetically, headings might be scattered around the document. No, I was just wondering why developers invented such convenient thing, but did not apply it to tag names. Philosophical question
  21. What if I should count all the words in a text, that is not in a single tag. For example, there are a few <p> tags: <p>first paragraph</p><p>second paragraph</p><p>third paragraph</p> function countwords(){ var p = document.getElementsByTagName("p"), n = document.getElementById("n"), a=[],text = "", count = 0; for(var x=0;x<p.length;x++){ text = p[x].innerHTML; text = text.trim(); a = text.split(" "); al = a.length; count = count + al; } n.innerHTML = count; } It works fine if I put a relatively small text. When I put there a pretty long text (around 300 words), the result is not correct. Does it count double or triple spaces between the words? But MS Word gives even higher result (374 instead of around 360 words).
  22. smus

    Counting the words in a text

    Already solved by changing innerHTML with innerText. Simple and easy
  23. agree with you, adding a class is the best solution in this case
  24. dsonesuk, you are right, adding a class is a reasonable solution. However, as far as I can get from a topic description, the author wants to address a <style> tag directly. Hypotheticaly, it must look like this: let style = document.getElementsByTagName("style")[0]; style.innerHTML = style.innerHTML + "{any selector we want to add here}";
  25. smus

    Onclick event

    dsonesuk, I used IE, UCBrowser, TouchBrowser on my WP. I decided to cancel all the changes I've made in the code and to check it on an Android device. Everything works perfectly! Seems they didn't update browsers for WP for a long period of time. Thanks, dsonesuk and Ingolme
×