Posts

Showing posts with the label arrays

Why wont my calculator function complete the math?

Why wont my calculator function complete the math? Hi so I'm having trouble figuring out why my function will do the division but leave the multiplication as an array without completing the math. Here's the code: const mathObj = { "*": function(a , b) {return a * b}, "/": function(a , b) {return a / b}, "+": function(a , b) {return a + b}, "-": function(a , b) {return a - b} } const arr = [ 10, "/" , 2 , "*" , 10 , "/" , 2 ]; function solveTheMath(arr) { const len = arr.length; for(let i = 0 ; i < len ; i++){ if(arr[i] === "/" || arr[i] === "*"){ const sliced = arr.slice(i - 1 , i + 2); var mathResult = mathObj[arr[i]](sliced[0], sliced[2]); arr.splice(i - 1 , 3, mathResult); console.log(arr); //[5*5] } } } solveTheMath(arr); Why does...

Remove Consecutive Duplicates Recursively giving infinite recursion

Remove Consecutive Duplicates Recursively giving infinite recursion Not a homework question. I am self learning. I have to remove consecutive characters in a string by recursion. However the program I made is not working for inputs containing duplicates. It is goining in infinite recursion and hence gives segmentation fault. However it is working for inputs which doesn't have consecutive duplicates in them. I have tried debugging in Eclipse Ide but things get weird when I debug. (I know how to debug) but I can't figure out the things are different when I debug and when I run. I will give you example after my code. #include <iostream> #include <cstring> using namespace std; void removeConsecutiveDuplicates(char *input) { int l = strlen(input); if(l == 0) { return; } if(input[0] != input[1]) { removeConsecutiveDuplicates(input+1); return; } int i = 1; for(; input[i] != ''; ++i) { input[i-1] = input[i]; ...

create element at index position if index does not exist in array

create element at index position if index does not exist in array I have an array objects that hold an id and a name const stages = [{ id: 1, name: '' }, { id: 2, name: '' }, { id: 3, name: '' }, { id: 4, name: '' }, { id: 5, name: '' }, { id: 6, name: '' }, { id: 7, name: '' }, { id: 8, name: '' }]; Further I have an array that holds numbers. const indexPositions = [0, 1, 2, 2, 2, 3, 2, 0]; I want to create a third array that holds arrays. Each number in distances represents the index of the current array within the array. distances If the current array does not exist yet I want to create it first. Obviously I have to create new arrays until I get to this index position. Example: My array is empty at start. The first index position is 0 so I have to create a new array for this. The next index position is 3 so I have to create more arrays until I have 4 arrays. All I want to do is to push t...

Javascript new array prototype in new object constructor

Javascript new array prototype in new object constructor If I do: Array.prototype.test = "test" Array.prototype.t = function() {return "hello"} Every new Array will have the property test and the method t . Array test t How can I do the same without affecting all Arrays? Like: Names = function(arr){ // Contacts constructor must be the same of Array // but add the property test and the function t } z=new Names(["john","andrew"]) So that z.test will return "test" and z.t() will return "hello" ? (but Array.test and Array.t would stay undefined ) z.test "test" z.t() "hello" Array.test Array.t undefined I explain better: Array.prototype.t="test"; Array.prototype.test = function(){ return "hello";} z=new Array("john", "andrew") console.log(z); But this affects ALL arrays. I want the same but with a new constructor Names that inherits Array constructor. ...

Xamarin.Android and RecordAudio: how to combine file writing and other task

Xamarin.Android and RecordAudio: how to combine file writing and other task I develop a Xamarin.Android app where I need to record an audio file in a .wav and to display the related spectrogram in the same time. I'm able to perform the 2 tasks separately but not to combine them. I use an AudioRecord to record the sound through the microphone: AudioRecord public async Task StartRecording() { audioRecord = new AudioRecord(AudioSource.Mic, 44100, ChannelIn.Mono, Android.Media.Encoding.Pcm16bit, bufferSize); if (audioRecord.State == State.Initialized) audioRecord.StartRecording(); isRecording = true; recordingThread = new System.Threading.Thread(new ThreadStart( WriteAudioDataToFile )); recordingThread.Start(); } The file writing is done in WriteAudioDataToFile() and works fine: WriteAudioDataToFile() private void WriteAudioDataToFile() { byte data = new byte[bufferSize]; string filename = GetTempFilename(); FileOutputStr...

Does the ECMAScript specification allow Array to be “superclassable”?

Does the ECMAScript specification allow Array to be “superclassable”? I'm looking for any indications whether or not "superclassing" a builtin type will work according to the specification . That is, given any hypothetical conformant implementation of ECMAScript, does "superclassing" a builtin break the runtime by affecting the creation algorithm of the class constructor? "Superclassable" , a term I'm coining, refers to a class whose objects returned by constructing it, or calling it as a function if applicable, will be created with the same internal slots (except for [[Prototype]]), regardless of what its direct superclass is, as long as the initial [[Prototype]] of the class constructor and the class prototype are still in each respective inheritance chain after reassigning them. Consequently, in order to be "superclassable", a class must not call super() during creation. super() When "superclassing" an Array , I would expec...

Two arrays in a loop with JavaScript

Two arrays in a loop with JavaScript I have a problem in my script. I have two arrays, which is the data and also a month. What I want is that to append two merge the month in data, but then only 1 value shows in month, here is my code: const months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']; ali_fcr.get().then((d) => { let store = ''; let getTotal = d.reduce(function(x, y) { return (x * 1) + (1 * y); }); let new_array = d.map((data) => { for (let i = 0; i < data.length; i++) { return '<td class="ta"><center><a class="ta" href="' + months[i] + '/ali">' + data + '</a></center></td>'; } }); new_array.unshift('<td>Case Receive</td>'); new_array.push('...

Store array and update record in database using codeigniter

Image
Store array and update record in database using codeigniter I am storing dynamic array as well as update the "max_quantity" table in database. The problem is that when update the record than it will sum all the quantity values and subtract it from the 1st value of "max quantity table". But I want that array of quantity is subtracted its own quantity value which is present in the database. This is my code in view <?php $i=1; foreach($result as $row){ ?> <td class="pr-right" style='width:130px; text-align: center; '> <input type="text" min="1" step="1" name="quantity" step="1" class="container" value="" onfocus="this.value = '';" onblur=";" style="width: 60px" id="quantityT<?php echo $i;?>" onkeyup="CalculatePrice (<?php echo $i;?>,<?php echo $row->max_quantity; ?...

Index out of range exception in 2D Array (C#)

Index out of range exception in 2D Array (C#) char[,] map = new char[10, 20]; for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; i < map.GetLength(1); j++) map[i, j] = '.'; } I just simply want to make all the elements of map[i,j] to be a point , but always when I try to run it the compiler says: Index out of range exception. Maybe it's a stupid question but I had to ask it. 2 Answers 2 See the i in your j-loop i for (int j = 0; j < map.GetLength(1); j++) Thank you!And that proves that it was a silly question, thank you again! – Jurás Bence Aug 26 '13 at 10:02 You use i instead of j look at this: i j char[,] map = new char[10, 20]; for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j <...

Take parts of two separate arrays and combine them into one

Take parts of two separate arrays and combine them into one I have two arrays. Each of them consist of "date" and "close" keys. Example: Array1 - Date: 12-Jun-18, Close: "55.6" Array2 - Date: 12-Jun-18, Close: "1.26" $stock_one_prices = sw::shared()->prices->getForStockID($id); $stock_one_prices_array = array(); foreach ($stock_one_prices as $stock_one_price) { $stock_one_prices_array = [ "date" => $stock_one_price['date'], "close" => $stock_one_price['close'] ]; } $stock_two_prices = sw::shared()->prices->getForStockID($idtwo); $stock_two_prices_array = array(); foreach ($stock_two_prices as $stock_two_price) { $stock_two_prices_array = [ "date" => $stock_two_price['date'], "close" => $stock_two_price['close'] ]; } I would like to take the two arrays and combine them into one, matching the dates of the ...

Tic tac toe C++ overwriting the board

Tic tac toe C++ overwriting the board Hi so I have it set up the way I want to for the most part. The last part I'm trying to implement is to have it so the input does not overwrite itself when input either X or 0 . If someone could take a look at it and tell me what exactly I did wrong with my noRepeat function I would greatly appreciate it. Thanks. X 0 #include <iostream> using namespace std; const int ROWS = 3; const int COLS = 3; char Player1 = 'X'; char Player2 = 'O'; int row, col; char board[3][3]= { '*', '*', '*', '*', '*', '*', '*', '*', '*'}; //Game board //Prototypes void noRepeat(); void drawBoard(); void selection(int&, int&); char winner(); void switchPlayer(); int main() { cout << "Tic Tac Toe!" << endl << endl; drawBoard(); cout << endl << endl; while (1) { if ( noRepeat() ) { ...