JavaScript – loop with do-while postcondition

In JavaScript, in addition to the while and for loops, there is another variant of the do-while loop:

do{
}while();

The meaning of this construct is that what is inside the curly braces is executed first, and then the while() condition is checked. If the condition is true, then the loop is repeated, if false, then the loop ends. Thus, the loop will be executed at least once. This loop is used less frequently than the previous two because it is easier for a human to understand a regular while loop to read the condition, rather than looking for the condition at the end of the loop. Continue reading →

Posted in Javascript (en) | Leave a comment

JavaScript – while and for loops

JavaScript has two loops, while and for. They both run at the same speed. The for loop is more flexible – it has three parts separated by semicolons in parentheses. General entry:

for(first; second; third);

Inside the parentheses, the semicolon is the delimiter, not the end of the line.

first – any instruction that the machine will execute once at the very beginning of the loop, for example, you can initialize a variable.

Continue reading →

Posted in Javascript (en) | Leave a comment

JavaScript – simple tasks for loops

Goal: write JavaScript code that calculates how much 2^10 is using a loop.

Solution:

varnum=2;
varpower=10;
varresult=1;
var subpower=1
while (subpower<=power){result*=num;subpower++};
document.write(result)

Continue reading →

Posted in Javascript (en) | Leave a comment

JavaScript – how to embed code in html and display on the site

JavaScript code can be written in any notepad and saved with a .js extension – you can also embed the code in an html page using the <script>…</script> tags in both the head and body sections. It is better to insert the body into the body of the site, this will speed up the loading of the content of the site itself.

Thus, you can create a file on your computer with the html or htm extension, in which you can prepare the following code:

<html>
<script>

your JavaScript code

</script>
</html>

Continue reading →

Posted in Javascript (en) | Leave a comment

JavaScript – Loops, Counter

A loop is a piece of code that repeats a certain number of times. The number of repetitions or the number of iterations is described with a counter.

The loop is written with while(an expression that is converted to a boolean type -> for the next iteration, you need to get true, as soon as we get false, the loop will stop);

An example of an infinite loop: while (true);

Which translates to false in JavaScript:

  • false
    undefined
    null
    0
    ” “
    NaN

Everything else is true.

Continue reading →

Posted in Javascript (en) | Leave a comment

JavaScript – data output

Inside the html code, you can insert and execute JavaScript using the <script>…</script> tags, which can be inserted into the head of the page.

Data output is different depending on the interpreter:

var cat = “vera”; //cat’s name
document.write(‘Hi, ‘ + cat); print the result to the user
print(‘Hi, ‘ + cat); //one output in the Visual console, but in the browser this function starts printing the page on the printer
console.log(‘Hi, ‘ + cat); //another output option, works in the Google Chrome console
Hi, Vera //result

How to use external file in code?

To connect files in JavaScript, use the src (search) function inside the tags <script src=”file name”>…</script>

Posted in Javascript (en) | Leave a comment

JavaScript – comments

A comment is a piece of code that is not interpreted. There are multi-line comments and single-line comments.

Multiline comment starts with /* and ends with */

JavaScript code
/*
a comment
for a few
lines
*/
JavaScript code

A single line comment starts with // and goes to the end of the line

JavaScript code //comment for one line

Comments in the code are needed for developers who may want to fix or complete something in the code. It is important to leave comments along the code so that users and other developers can easily understand the code.

Posted in Javascript (en) | Leave a comment

JavaScript – good manners

In JavaScript, it is customary to name variables in small letters (if the name is one word) – cat; If the variable name consists of two words, then camel case notation is used, i.e. the first word is written in small letters, and the second is glued to the first and written with a capital letter: myCat; If there are several words, then the first one is written with a small letter, and each new word is glued with the previous ones and is written with a capital letter: myDearCat;

It is customary to declare variables using the var keyword, i.e. each new variable must be written on a separate line: var cat;

Continue reading →

Posted in Javascript (en) | Leave a comment

JavaScript – Basic Operators

Comparison operator > / <

If we write 2 < 6 , we thus ask the question, is it true or not? The answer can be in the form of boolean values – True / False.

true > false

Equality operator (soft equals) ==

We ask, 6 == 6 (six equals six), the answer will be true

We cannot compare variables of different types, that is, we cannot compare a number and a string. If we write 6 == “6” then we are asking if the number six is equal to the string six. In this case, we will also get true, because the string will be converted to a number.

Continue reading →

Posted in Javascript (en) | Leave a comment

JavaScript – first impression

Java programming language or Java script – JavaScript – was created by a fun programmer in 10 days. Almost as God created the Earth, so Brendan Eich created a programming language in 1995 that is compatible with all browsers and is still the most popular programming language for web applications and websites.

JavaScript language features:

  • interpretive
  • register (case) sensitive
  • embedded in html, etc.

The core of the language is ECMAScript-262, where the main constructs and syntax are described.
Continue reading →

Posted in Javascript (en) | Leave a comment

Python OOP – Object Oriented Programming

We continue to practice programming. After the eighth chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned the principles of OOP and program objects / classes, it’s time to move on to practice. Let’s do our homework together!

A Brief Summary of OOP in Python

A program object is a formal representation of a real object in a programming language. Objects are created based on classes.
Continue reading →

Posted in Python (en) | Leave a comment

How to rename a file extension in Python

Task: there is a file with the .raw extension in the folder and I need to rename this file to the same one, but with a different .txt extension – how can I do this?

Solution:
You need to import the os module, which has the os.path.splitext() and os.rename() methods. Using the os.path.splitext() method, you can split the source file into two parts – its name and permission, which falls into square brackets as [0] – name and [1] – permission. Next, you just need to glue the name with the new permission via os.rename(). Continue reading →

Posted in Python (en) | Leave a comment

Python: Problems and Solutions (Chapter 7 Files and Exceptions Quiz Game)

We continue to practice programming. After the seventh chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned how to work with files, it’s time to move on to practice. Let’s do our homework together!

A short summary of working with files in Python

Open and close a file

open() – open file and give directory path;
open("file", "r", encoding="utf-8") – open file for reading in Unicode encoding. Continue reading →

Posted in Python (en) | Leave a comment

Python Error: inconsistent use of tabs and spaces in indentation

When running the code, a syntax error occurs – inconsistent use of tabs and spaces in indentation. What does this mean and how to solve this problem? The fact is that python enforces uniformity in the use of spaces, which show the indentation of parts of the code. If you use tab, it can get tricky because some of the tabs can be misinterpreted as spaces. As a result, you see a syntax error that can be difficult to fix. Continue reading →

Posted in Python (en) | Leave a comment

Python: Problems and Solutions (Chapter 6. Functions. Tic-Tac-Toe Game).

We continue to practice programming. After the sixth chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned how to use functions, it’s time to move on to practice. Let’s do our homework together!

Function refinement ask_number() / hod_number()

Task: Modify the ask_number() or hod_number() function so that it can be called with one more parameter – multiplicity (step size). Set the default stride to 1.
Continue reading →

Posted in Python (en) | Leave a comment

Python: non-standard functions. Game “Tic-tac-toe”.

We continue to learn the Python programming language Python. Let’s move on to chapter 6 “Functions. Tic-Tac-Toe Game” from the book: Michael Dawson “Python Programming”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition) to create our own functions and work with global variables .

How to create a function?

General view of a function in Python: function_name()

To create your own function, you need to declare it.

General form of a function declaration

def function_name(parameter1, parameter2...):
    '''Document string'''
  Function expression block

Continue reading →

Posted in Python (en) | Leave a comment

Python: Problems and Solutions (Chapter 5 Lists and Dictionaries Hangman Game).

We continue to practice programming. After the fifth chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned how to make dictionaries and use lists, it’s time to move on to practice. Let’s do our homework together!

Task: Write a program that will display a list of words in random order. All words from the presented list should be printed on the screen without repetition.
Continue reading →

Posted in Python (en) | Leave a comment