Jump to content

smus

Members
  • Content Count

    127
  • Joined

  • Last visited

Everything posted by smus

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

    Array declaration

    a() redim a(n) now it works, thanks
  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

    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?
  12. 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; }
  13. 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
  14. 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).
  15. smus

    Counting the words in a text

    Already solved by changing innerHTML with innerText. Simple and easy
  16. agree with you, adding a class is the best solution in this case
  17. 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}";
  18. 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
  19. smus

    Onclick event

    So onclick event on a desk-, laptop computers does the same as when we tap on an element on our mobile devices, right? I always thought these two actions are totally similar. Why then the function might not be working 'on tap', however everything works fine on PC? I checked the browser version on a mobile device, it supports HTML5 tags. I also tried to create a simple function showing message using 'alarm' and it also works. What might be a possible reason?
  20. smus

    Onclick event

    Thanks, dsonesuk, I tried to transform the code as you've suggested. It doesn't work on my mobile device. If it is working on Android, it must be because the browser on the phone does not fully support HTML5. This code: <audio controls> <source src="../audio/smex.wav"> Your browser does not support the audio tag. </audio> is working on Desktop, but gives the 'Invalid source' on the phone.
  21. smus

    Onclick event

    Changed - absolutely the same. This code also works on the web, but does not on a mobile device: <audio class="newAudio" src="../audio/femalelaughter.mp3"></audio> <strong onclick="playSound()">alink</strong> <script> function playSound(){ const audio = document.querySelector(".newAudio"); audio.currentTime = 0; audio.play(); } </script> What else? I also tried different browsers on my phone. I have Windows Phone, can anyone try it on iPhone or Android?
  22. smus

    Onclick event

    Commented this string: document.getElementById(a).classList.add('playing'); Erased the ids from all the <audio> tags´╗┐. It is the same: there is no sound. I even tried to include audio tag independently and check whether it is working on my mobile device, and it was working. I think I need to check my function in a different way. Might there be the absence of EcmaScript support by the mobile device browser? I suspect that it may not work because of how it takes the data-key parameter from the audio tag: const audio = document.querySelector(`audio[data-key="${a}"]`);
  23. smus

    Onclick event

    Exactly, you are right. I am using duplicate IDs here only for getting them from inside of the tag, the function uses data-keys. Here is how I am checking: <strong id="76" onclick="alert(this.id)">L is for Laugh</strong><br> It works on both PC and mobile
  24. smus

    Onclick event

    No IDs are different. I've just tried putting an 'alert' directly onclick, and it worked. The function doesn't work at all, no matter what's inside.
  25. smus

    Onclick event

    Oh, yes, right, it is 'alert'. There are some <audio> tags involved, and my function makes one of them start playing, depending on data-key id: <strong id="76" onclick="makeLaughM(this.id)">L is for Laugh</strong><br> <audio id="76" data-key="76" src="audio/smex.wav"></audio> <audio id="65" data-key="65" src="audio/laugh.mp3"></audio> <audio id="85" data-key="85" src="audio/laugh2.mp3"></audio> <audio id="71" data-key="71" src="audio/snortlaugh.mp3"></audio> <audio id="72" data-key="72" src="audio/femalelaughter.mp3"></audio> <script> function makeLaughM(a){ const audio = document.querySelector(`audio[data-key="${a}"]`); audio.currentTime = 0; audio.play(); document.getElementById(a).classList.add('playing'); document.getElementById('facecolor').style.fill = "orange"; } </script>
×