---
# System prepended metadata

title: Exercices Javascript Algo.
tags: [algorithmie]

---

---
title: Exercices Javascript Algo.
tags: algorithmie
robots: noindex, nofollow
author: Julien Noyer
---

# Exercices Javascript Algo.

## Exercice 1

Trouveer les nombres pairs et impairs du tableau suivant :

```
[65, 2, 87, 30, 16, 5, 14, 67, 56, 8]
```

### Pseudo code

```
ALGORITHME exo1
    result: STRING
    data: ARRAY<INTEGER>

START
    result <- ''
    data <- [65, 2, 87, 30, 16, 5, 14, 67, 56, 8]
    
    FOR i FROM 0 TO data.LENGTH [ i <- i + 1 ] 
        IF data[i] % 2 === 0 THEN
            result <- result + data[i] + ' = pair'
        ELSE
            result <- result + data[i] + ' != pair'
        END IF
        
    END FOR
    
    PRINT(result)
   
END
```

### Javascript


```js
/* Declaration */
let result = '';
let data = [65, 2, 87, 30, 16, 5, 14, 67, 56, 8];

/* Use the For Of loop */
for( item of data){
    // If we can divide item value by 2
    item % 2 === 0 
    ? result += `${item} = pair ` // its peer
    : result += `${item} != pair ` // its odd
}

// Display result
console.log(result)
```

### Python

```python=
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = ''

def pair():
    for myNumber in myList:
        if myNumber%2 == 0:
            result += myNumber + ' = pair'
        else:
            result += myNumber + ' != pair'
            
    print (result)
    
pair()
```

## Exercice 2

Touver le plus petit, le plus grand la moyenne des chiffres suivants :

```
[65, 2, 87, 30, 16, 5, 14, 67, 56, 8]
```

### Pseudo code

```
ALGORITHME exo2
    data: ARRAY<INTEGER>
    total: INTEGER
    min: INTEGER
    max: INTEGER


START
    total <- 0
    data <- [65, 2, 87, 30, 16, 5, 14, 67, 56, 8]
    
    FOR i FROM 0 TO data.LENGTH [ i <- i + 1 ] 
        total <- total + data[i]
        
        IF i === 0 THEN
            min <- data[i]
            max <- data[i]
            
        ELSE IF data[i] < min THEN
            min <- data[i]
            
        ELSE IF data[i] > max THEN
            max <- data[i]
        END IF
        
    END FOR
    
    PRINT('Moyenne : ' + (total / data.LENGTH) + ' - Min. : ' + min + ' - Max. : ' + max )
END
```

### Javascript

```js
/* Declarations */
let data = [65, 2, 87, 30, 16, 5, 14, 67, 56, 8];
let total = 0;
let min;
let max;

// Use For Each loop with iteration index
data.forEach( (item, i) => {
    // Add item value to total
    total += item;

    // Set min and max values at the first loop
    if( i === 0 ){ min = item; max = item }
    // If current item value is smaller than min value
    else if( item < min ){ min = item }
    // If currrent item value is bigger than max value
    else if( item > max ) { max = item };
});

// Display result: divide total value by data length value
console.log(`
    Moyenne : ${total / data.length} - Min. : ${min} - Max. : ${max}
`);
```

### Python

```python=
myList = [65, 2, 87, 30, 16, 5, 14, 67, 56, 8]

def moyenne():

    total = 0
    myMin = 0
    myMax = 0
    
    for myNumber in myList:
        total = total + myNumber
        
    my_moyenne = total / len(myList)
    
    myMin = min(myList)
    myMax = max(myList)
    
    print ('Moyenne : ', my_moyenne, ' min : ', myMin, ' - max : ', myMax)
    
moyenne()
```

## Exercice 3

Additionner lees chiffres suivants `123456789` et afficher dans la console `1 + 2 + 3 + .... `

```
ALGORITHME exo3
    data: STRING
    total: INTEGER
    result: STRING
    
START
    total <- 0
    data <- '123456789'
    
    FOR i FROM 0 TO data.LENGTH [ i <- i + 1 ] 
        total <- total + PARSEINT(data[i])
        result <- result + data[i]
        
        IF i !== data.LENGTH - 1 THEN
            result <- result + ' + '
        ELSE
            result <- result + ' = '
        END IF
    
    END FOR
    
    result <- result + total

    PRINT(result)
   
END
```

```js
// Declaration
let data = '123456789'; // => :Array<String>
let total = 0;
let result = '';

// Use the For loop
for( let i = 0; i < data.length; i++ ){
    // Parse value in Integer and add it to total
    total += parseInt(data[i]);
    // Add value in result
    result += `${data[i]}`;

    // Check if current iteration isn't thee last one
    i !== data.length - 1 
    ? result += ' + ' // not the last one
    : result += ' = '; // the last one
}

// Display result
console.log(result += total);
```

### Python

```python
def addition():
    list = 123456789
    i = 0
    total = 0
    my_addition = []
    list_len = len(str(list))
    
    while i != list_len:
        total = total + int(str(list)[i])
        my_addition.append(str(list)[i])
        i += 1
        
    print(' + '.join(my_addition) + ' = ' + str(total))

addition()
```