loops

counting how many times an item appears in a multidimensional array in javascript

送分小仙女□ 提交于 2021-02-17 02:03:11
问题 Given a multidimensional array like this: var arr = [ "apple", ["banana", "strawberry","dsffsd", "apple"], "banana", ["sdfdsf","apple",["apple",["nonapple", "apple",["apple"]]]] ,"apple"]; the challenge was to write a function that keeps the array and an item as arguments and return how many times that item appears in the array. My first solution did the trick: function countItems(arr, item, sumarr = []) { // sumarr = array to store items matched for (let i = 0; i < arr.length; i++ ){ //

How to use a secondary alphabetical sort on a string list of names?

≯℡__Kan透↙ 提交于 2021-02-16 21:00:58
问题 I'm trying a way that when given a string of names which are first and last names, where names are split by ; and first name split to last name by : -> "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill" I want to return a string which is sorted out all the names as uppercase and sorted alphabetically according to the last name and the ones that share the same last name then sort again (secondary sort) between the first names of the

How to use a secondary alphabetical sort on a string list of names?

♀尐吖头ヾ 提交于 2021-02-16 20:59:53
问题 I'm trying a way that when given a string of names which are first and last names, where names are split by ; and first name split to last name by : -> "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill" I want to return a string which is sorted out all the names as uppercase and sorted alphabetically according to the last name and the ones that share the same last name then sort again (secondary sort) between the first names of the

Change background color with a loop onclick

人走茶凉 提交于 2021-02-16 20:58:34
问题 here is my js fiddle : http://jsfiddle.net/pYM38/16/ var box = document.getElementById('box'); var colors = ['purple', 'yellow', 'orange', 'brown', 'black']; box.onclick = function () { for (i = 0; i < colors.length; i++) { box.style.backgroundColor = colors[i]; } }; I'm in the process of learning JavaScript. I was trying to get this to loop through each color in the array, but when i click the box (demonstration on jsfiddle) it goes to the last element in the array. 回答1: Here are two methods

Change background color with a loop onclick

喜欢而已 提交于 2021-02-16 20:58:06
问题 here is my js fiddle : http://jsfiddle.net/pYM38/16/ var box = document.getElementById('box'); var colors = ['purple', 'yellow', 'orange', 'brown', 'black']; box.onclick = function () { for (i = 0; i < colors.length; i++) { box.style.backgroundColor = colors[i]; } }; I'm in the process of learning JavaScript. I was trying to get this to loop through each color in the array, but when i click the box (demonstration on jsfiddle) it goes to the last element in the array. 回答1: Here are two methods

Change background color with a loop onclick

被刻印的时光 ゝ 提交于 2021-02-16 20:57:53
问题 here is my js fiddle : http://jsfiddle.net/pYM38/16/ var box = document.getElementById('box'); var colors = ['purple', 'yellow', 'orange', 'brown', 'black']; box.onclick = function () { for (i = 0; i < colors.length; i++) { box.style.backgroundColor = colors[i]; } }; I'm in the process of learning JavaScript. I was trying to get this to loop through each color in the array, but when i click the box (demonstration on jsfiddle) it goes to the last element in the array. 回答1: Here are two methods

C++ Continue Statement Confusion

不羁的心 提交于 2021-02-16 20:44:35
问题 Background I was reading an old C++ primer that I have had lying underneath my bed for a few years. While reading a particular chapter, I happened to come across the " continue " statement. I read the information in the book about it; however, the book was a little short on details. I, being curious, wanted to test the continue statement to see if I could figure out more about it on my own (although it may be obsolete now, I still am curious on how it works). Problem I read that the continue

Recursion output

元气小坏坏 提交于 2021-02-16 20:33:49
问题 I was just introduced to recursion and I was given the following lines of code: public class RecursionThree { public void run(int x ) { if(x<5) run(x+1); out.println(x); } public static void main(String args[] ) { RecursionThree test = new RecursionThree (); test.run(1); } } and the output is supposed to be: 5 4 3 2 1. I get why it would print 5 (because 5<5 would equal false and it would print x, which is 5). However, I do not understand why it prints 4 3 2 1 too. Thanks for your help 回答1:

Accessing Python dict keys with or without dict.keys()

偶尔善良 提交于 2021-02-16 17:53:47
问题 Usually I access dict keys using keys() method: d = {'a':1, 'b':2, 'c':3} for k in d.keys(): print k But sometimes I see this code: for k in d: print k Is this code correct? safe? 回答1: Although this was already mentioned, I wanted to add some exact numbers to these discussion. So I compared: def iter_dtest(dtest): for i in dtest: pass and def list_dtest(dtest): for i in dtest.keys(): pass A dictionary with 1 000 000 items was used (float keys) and I used timeit with 100 repetitions. These are

How to avoid loops when writing cloud functions?

两盒软妹~` 提交于 2021-02-16 13:36:49
问题 When writing event based cloud functions for firebase firestore it's common to update fields in the affected document, for example: When a document of users collection is updated a function will trigger, let's say we want to determine the user info state and we have a completeInfo: boolean property, the function will have to perform another update so that the trigger will fire again, if we don't use a flag like needsUpdate: boolean to determine if excecuting the function we will have an