Solidity Basics

Hi @sohamdey_2006,

Apologies in the delay in answering your question…

I imagine you have already resolved this now, but just in case…

You need to add return to return the value:

return balance[msg.sender];

This should have generated an orange/yellow compiler warning. Always check the compiler’s warning messages (as well as the error messages) because they will still notify you about important modifications that you should make.

Hi @gioteam,

The reason why it’s not returning 20 plus the initial number input is the extra line you’ve added to your loop body: i++
You already have this in the for-loop header, and so i will now increase by 2 on each loop instead of 1, which means the loop will only iterate 5 times before exiting, instead of 10, as follows:

number += 2   performed on each iteration
count() function called with 0
i = 0   =>  iteration 1   number =  2
i = 2   =>  iteration 2   number =  4
i = 4   =>  iteration 3   number =  6
i = 6   =>  iteration 4   number =  8
i = 8   =>  iteration 5   number = 10
i = 10 =>  i < 10 evaluates to false => loop is exited
returns 10 instead of 20

So this is the code you need for the for-loop

for (int i = 0; i < 10; i++) {
    number += 2;
}

The code in your screen shot will only return 5 plus the initial number input, because it’s performing   number++   on each iteration, instead of   number += 2

Just one other observation…
Your count() function should be marked pure instead of view , because it doesn’t need to modify or read the contract state. The compiler should have generated an orange/yellow warning for this.

Just let me know if you have any further questions :slight_smile:

Hey @jak,

Sorry for the delay in getting back to you.

No… you will have enough programming knowledge to do this Ethereum Smart Contract 101 course if you have already completed the JavaScript Programming course. The React Development course is definitely a useful course to do (for frontend dapp development) but not required knowledge for this course.

I’ve just seen you’ve already finished this course! So, I imagine you’ve already found this out for yourself, anyway :wink:

I would recommend that you take your time with these Solidity courses, and really go through the material thoroughly, experiment and practise with the code, and do your own research.

1 Like

Hi @david.maluenda,

The Person struct instances are stored sequentially in an array (people). A value stored in any array is referenced with its index number. This is different to a mapping, where each value is mapped to a key, which, instead of a number, could be an address, for example.

The _index argument passed to getPerson() is the array index number of the Person instance you want to reference. In the following line of code, this Person instance is first stored temporarily in a local variable personToReturn.

Person memory personToReturn = people[_index];

Then, in the return statement, individual property values of this Person instance (name and age) are referenced and returned. This could also be done without the local variable, as follows:

function getPerson(uint _index) public view returns(uint, string memory) {
    return(people[_index].age, people[_index].name);
}

Just let me know if anything is still unclear, or if you have any further questions :slight_smile:

Sure did - it didn’t disappoint!
I will be going through every course in the academy and build a few projects along the way so was just deciding the best way to structure the order.

The whole course was essentially Solidity and Remix so that was good. Thought maybe we’d be building front-ends for the contracts but obviously that comes in the Ethereum Dapp course.

I know that’s a good idea - I’m so antsy to get to some of the higher-level Ethereum courses that I’ve completed 10 courses in the last 30 days :confused: For sure wayyy more competent than a month ago, but definitely need to go back through the courses one or two more times aha.

1 Like

Correct… although, that course currently uses vanilla (standard) JavaScript and jQuery to build the frontend… However, the overall thought processes, steps and considerations will be broadly the same using React to build your frontend, and so the skills you learn in the Ethereum Dapp Programming course will be transferable to then building a frontend using React once you’ve done that course as well.

Glad you enjoyed the course! :smiley:

Interesting!
Maybe should’ve left React for later then - the content so far is a bit more dense than the other programming courses so far imo.
At a beginners level I found jQuery to be more digestible than React, albeit less flexible/functional.

Can’t hurt to get a start in any case and will no doubt incorporate React into the Dapp course as well.
Thanks for helping out! :grin:

1 Like

But if it’s frontend development you’re more interested in, it’s definitely worth continuing and persisting with afterwards, because React is currently what is more in-demand for dapp frontend development. That’s why the React course was added.

That’s what my recommendation would be. After completing the dapp course using jQuery, revisit React, and use it to build an alternative version of your frontend :slight_smile:

Super stoked it’s here. To be a full-stack founder/dev it’s an important skill to have so will stick with it.
Even if it’s not as exciting as some of the other web 3.0 stuff :stuck_out_tongue:

1 Like

Can a mapping have a key which is another mapping :thinking:, is that possible and recommended if required in any scenario.

This post has well explained about mappings: https://medium.com/coinmonks/solidity-tutorial-all-about-mappings-29a12269ee14

2 Likes

No it’s not possible. The table in the section Key and value types allowed, in the article you’ve linked to, confirms that. However, as the table also confirms, a mapping can have another mapping as its value type i.e. the data type stored in the mapping and mapped to the key. This creates a mapping nested within a mapping, and Nested Mappings is another section in the article.
Note that a mapping can only store multiple mappings which all have the same key/value definition e.g.

mapping(address => mapping(address => bool)) private myNestedMapping; 

This was extremely helpful, Jon! Thanks. I like getting clarification on a line of thought :slight_smile:

1 Like

Hello!

Im having some trouble… when i deploy my Contract i am always getting - as decoded ouput. The function im trying is a simple if, else:

pragma solidity 0.7.5;

contract example {

function controlflow() public view returns(string memory){
    
    if(msg.sender == 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4){
        return "DANI";
    }
    else{
        return "nothim";
    }
    
}

}

And the result:

image

Thank you very much in advanced!

Hey @daninvest,

I’ve copied your exact code into Remix and deployed it.

I think you may be opening the wrong receipt in the Remix console. The first one will be a transaction receipt with a green tick; but this is for the contract deployment…

// RESULT =>
decoded output      -

First, I called the controlflow() function with the default address (0x5B3…dC4) by clicking on the blue button. As this function is a getter, and doesn’t modify the contract state, it does not generate a transaction that will be mined or included in the blockchain. It also doesn’t cost any gas. You need to open the CALL receipt which doesn’t have a green tick …

// RESULT =>
0:   string: DANI
decoded output     { "0": "string: DANI" }

Then, I called the controlflow() function with a different address — the second address in the Account dropdown (0xAb8…cb2). Again, this will generate a CALL receipt without a green tick …

// RESULT =>
0:   string: nothim
decoded output     { "0": "string: nothim" }

Just let me know if you’re still having problems…

1 Like

My head spun on the first lesson :woozy_face:

but ill catch on :grin:

2 Likes

What does it mean that contract is deployed?

is it just putting the contract to blockchain?

Hi Guys,
I write:
// SPDX-License-Identifier: MIT
pragma solidity 0.7.4;

contract HelloETH {

string message;

constructor(string memory _message){
    message = _message;
}

function hello(int number) public view returns(string memory){
    if(number == 10){
        return message;
    }
    else{
        return "You are not welcome here";
    }
    return message;
}

}
and had such a message:
contracts/controlFlow101.sol:19:9: Warning: Unreachable code.
return message;
^------------^
I understand it, but wonder why and the lesson about Control Flow Filip had not had such a message.
Any suggestion?

Hey @Kacper, hope you are ok.

The only issue is on the last return in your hello function.
return will end the function execution (its the last process you use on a function, to return the result)

The conditionals if and else will set the result of the function operation. The problem here is that, IF the condition (number == 10) is true, it will return the message.
ELSE will be triggered for any other condition different than the IF one.

So the issue is on the last return message, which is not need it because you already will end the execution of the function on the ELSE condition.

Hope I explained my self clear, if not let me know :nerd_face:

Carlos Z

yes I understand, but on the ETH101 course Filip had also like that and didnt have a error, why?