---
# System prepended metadata

title: Teh 6

---

---
title: 'Teh 6'
---

Teht 6 Laivanupotus
===


## Table of Contents

[TOC]

## Rest Backend

Tehtävässä luon 5 arrayta, ja arvon switch loopilla 3 peräkkäistä joihin laitetaan laivan merkiks "0"

Koodi
---

```
const express = require('express');
const app = express();
app.listen(3000, function () {
    console.log('Node app is running on port 3000');
});
module.exports = app;


app.get('/laiva', function (req, res) {
 var arr = [1,1,1,1,1]
 var arr2=[1,1,1,1,1]
 var arr3 = [1,1,1,1,1]
 var arr4 = [1,1,1,1,1]
 var arr5 =[1,1,1,1,1];
 const rand = Math.random() < 0.5 //false laiva vaaka, true laiva pysty
if (rand == false) {
    console.log (rand + "rand")
var rivi = Math.floor((Math.random() * 5)+1);
var alku = Math.floor((Math.random() * 3))
console.log (rivi + "rivi")
console.log (alku + "alku")
switch(rivi) {
    case 1:
        arr[alku ] = 0;
        alku++;
        arr[alku ] =0;
        alku++;
        arr[alku ] = 0
      break;
    case 2:
            arr2[alku] = 0;
            alku++;
            arr2[alku] = 0;
            alku++;
            arr2[alku] = 0
      break;
    case 3:
            arr3[alku] = 0;
            alku++;
            arr3[alku] = 0;
            alku++;
            arr3[alku] = 0
      break;
    case 4:
            arr4[alku] = 0;
            alku++;
            arr4[alku] =0;
            alku++;
            arr4[alku] =0
      break;
    case 5: 
    arr5[alku] =0;
    alku++;
    arr5[alku] = 0;
    alku++;
    arr5[alku ] = 0;
      break;
    default:
      // code block
  } 

} else {
    var rivi = Math.floor((Math.random() * 5));
var alku = Math.floor((Math.random() * 3))
console.log(rand + "rand")
console.log(alku + "alku")
console.log (rivi + "rivi")
    switch(alku) {
        case 0:
            arr[rivi ] = 0;           
            arr2[rivi ] =0;           
            arr3[rivi ] = 0
          break;
        case 1:
                arr2[rivi] = 0;               
                arr3[rivi] = 0;
                arr4[rivi] = 0
          break;
        case 2:
                arr3[rivi] = 0;
                arr4[rivi] = 0;
                arr5[rivi] = 0
          break;
        default:
      } 

}

    if (arr == null) {
      return res.status(400).send({ error:true, message: 'Jotain meni vikaan..' });
    }

    return res.send({ error: false, data: arr , arr2 , arr3 ,arr4,arr5, message: 'Laiva luotu onnistuneesti.' });
   
});

app.use(function(req, res, next) {

    res.header("Access-Control-Allow-Origin", "*");
   
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
   
    next();
   
    });



```

Esimerkkinä tietojen päivitys index.html documenttiin