Jump to content


  • Content Count

  • Joined

  • Last visited

Community Reputation

0 Neutral

About smus

  • Rank
  1. 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(){} }
  2. 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!
  3. 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.
  4. 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?
  5. 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/
  6. 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?
  7. 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/
  8. smus

    Array declaration

    a() redim a(n) now it works, thanks
  9. 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?
  10. 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?
  11. 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; }
  12. 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
  13. 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?
  14. smus

    Counting the words in a text

    Already solved by changing innerHTML with innerText. Simple and easy
  15. 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).