How to avoid declaring variable outside of enhanced for loop

I want to avoid using variable index outside of enhanced for loop as it is useless after the loop and pollutes the namespace but cannot find my way around it. Problem int index = 0; // I want to avoid using this variable outside the loop for (List<Integer> bucket : buckets) { for (int el… Read More How to avoid declaring variable outside of enhanced for loop

How to simplify this code where a variable is declared after an user input in c++?

I wrote the following code : #include <iostream> using namespace std; class Shape { public : float lgth; public : void getLgth() { cin >> lgth;} }; class Square : public Shape { public : float calcArea() { return lgth * lgth; } }; class Circle : public Shape { public : float calcArea() {… Read More How to simplify this code where a variable is declared after an user input in c++?

How should I write the JavaScript code such that the output will be "first", "second" and "third"

I’m trying to figure out a JavaScript code scope problem and I have simplified this to const first = () => { console.log("first"); } const second = (first) => { first(); console.log("second"); } const third = (second) => { second(); console.log("third"); } Based on three functions above, I am supposed to write code using only… Read More How should I write the JavaScript code such that the output will be "first", "second" and "third"

"FATAL ERROR: SIGABRT" when defining a string constructor in a custom String class

This error occurred FATAL ERROR: test case CRASHED: SIGABRT – Abort (abnormal termination) signal ::error::Error: Exit with code: 134 and signal: null when I was testing a string constructor "String::String(const char* other)" String::String(const char* other) // the constructor I’m talking about { if (other == nullptr) { String(); } else if (other != nullptr) {… Read More "FATAL ERROR: SIGABRT" when defining a string constructor in a custom String class

C++ pointer to std::string updated in function – scope issue? (ESP32)

I have a std::string and pass that by reference to a function. The function dereferences the parameter (pointer) into a local std::string for ease of working. I update the local string’s value and then update the pointer. Have I just pointed to something that’s gone out of scope? Or have I updated the underlying value… Read More C++ pointer to std::string updated in function – scope issue? (ESP32)

Ruby: Working with instance variables within a case statement

I’m perplexed as to why I need to selectively refer to an instance variable with "self" inside a case statement inside a class method: I have a class with an instance method @working_dir: class FileSystem attr_accessor :sizes, :working_dir attr_reader :input def initialize(input) @input = input.split("\n") @sizes = Hash.new(0) @working_dir = [] end … end I’ve… Read More Ruby: Working with instance variables within a case statement