Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LukeSkyRunner/765743cd936b0d9653e49a829f7710b9 to your computer and use it in GitHub Desktop.
Save LukeSkyRunner/765743cd936b0d9653e49a829f7710b9 to your computer and use it in GitHub Desktop.
Javascript Guided Example + Practice
//Define function welcome() that will receive one argument, your name, and greet you with your name.
function sayHello (name){
console.log (`Hi! My name is ${name}`);
}
sayHello (`Luca`);
//Define a function printToTwenty() that will print the numbers from 0 to 20 to the console ( don’t forget we have to invoke/call the function to see it working )
function printToTwenty(){
for (i=0; i<=20; i++){
console.log (i);
}
}
printToTwenty();
//Define a function printNumbers() that receives one argument and prints the numbers from 0 to whatever number we pass to it as an argument
function printNumber(num){
for (i=0; i<=num; i++){
console.log (i);
}
}
printNumber(12);
//Define a function printArrElements(someArr) that will loop through array and print all the elements of that array. Use the following array to pass it as an argument to the function you’ve just defined:
//Define an array of your favorite cities and pass it as an argument to the previously defined function. Invoke a function.
//efine another array of your favorite food and pass it to this function when invoking it.
//Define a function printEvens(someArr) and use the ironCities array and print only its even elements.
let ironCities = ["Amsterdam", "Barcelona", "Berlin", "Lisbon", "Madrid", "Mexico City", "Miami", "Paris", "Sao Paulo" ];
let favoriteFoods = ["Pizza","Sushi","Cola","Oreo","salami","cheese","apple","orange"];
let favoriteCities = ["Lisbon","Rome","El Paso","Cairo","London","Oslo"];
function printArrElements(arr){
for (i=0; i<arr.length; i++){
console.log (arr[i]);
}
}
printArrElements(ironCities);
printArrElements(favoriteFoods);
printArrElements(favoriteCities);
//Define a function printEvens(someArr) and use the ironCities array and print only its even elements.
function printEvens(arr){
for (i=0; i<arr.length; i++){
if (i % 2 === 0) {
console.log (arr[i]);
}else {
continue
}
}
}
printEvens(ironCities);
//Define a function that will receive an array as an argument and calculate a sum of all its elements. Example, if we pass the following array to that function: const prices = [5, 7.99, 9.99, 0.99, 21], it should return 44.97 as output. How would you concatenate $ sign in front of the sum?
const prices = [5, 7.99, 9.99, 0.99, 21];
function arraySum(arr){
let sum = 0;
for (i=0; i<arr.length; i++){
sum += arr[i];
}
console.log (`The sum of the Array is ${sum}`);
}
arraySum(prices);
//Define a function stringToLetters() that receives a string as an argument and returns an array of its letters.
function stringToLetters(string){
let x = string.split("");
console.log (x);
}
stringToLetters("Hello");
//Define a function that will: count from 1 to 100,
//on numbers divisible with 4 print “Hey”,
//on numbers divisible with 6 print “There”,
//on numbers divisible with both 4 and 6 print “Ironhack”,
//skip numbers divisible with 7,
//on the number that represents your age add ! (ex. 34!).
function count(){
for (let i=0; i<100; i++){
if (i % 4 ===0) {
console.log ("Hey");
}
else if (i % 6 === 0) {
console.log ("There");
}
else if (i % 4 === 0 || i % 6 === 0){
console.log ("Ironhack");
}
else if (i % 7 === 0){
continue
}
//Trying to do but CodePen crash dkw:
//else if (i = 25){
//console.log (`${i}!`);
//}
else {
console.log (i);
}
}
}
count()
@spaterman
Copy link

Thank you for the results.
for the last one:

function count(){
for (let i=0; i<100; i++){
if (i % 4 ===0) {
console.log ("Hey");
}
else if (i % 6 === 0) {
console.log ("There");
}
else if (i % 4 === 0 && i % 6 === 0){
console.log ("Ironhack");
break;
}
else if (i % 7 === 0){
break;
}

else {
  console.log (i);
}

}
}
count()

@juliapsrk
Copy link

Regarding the last one -> add "!" behind your age:

function count(){
for (let i=0; i<100; i++){
if (i % 7 === 0) {
continue;
} else if (i % 4 === 0 && i % 6 === 0) {
console.log("Ironhack");
continue;
} else if (i % 6 === 0) {
console.log ("There");
} else if (i % 4 ===0) {
console.log ("Hey");
} else if (i === 25) {
console.log(${i}!);
} else {
console.log (i);
}
}
}
count();

@dibrandon
Copy link

fixed function coun:
Remember the instructions!

it have to count from 1 not from 0 until 100 so the "for loop" is "for (let i = 1; i < 100; i++)"
-also, code from the less to the general (instruction), the order matters
Define a function that will:
count from 1 to 100,
on numbers divisible with 4 print “Hey”,
on numbers divisible with 6 print “There”,
on numbers divisible with both 4 and 6 print “Ironhack”,
skip numbers divisible with 7,
on the number representing your age add ! (ex. 34!).
"
function count() {
for (let i = 1; i < 100; i++) {
if (i === 32) {
console.log(${i}!); <-- put backtick ``

    } else if (i % 7 === 0) {
        continue;

    } else if (i % 4 === 0 && i % 6 === 0) {
        console.log("Ironhack");

    } else if (i % 6 === 0) {
        console.log("There");

    } else if (i % 4 === 0) {
        console.log("Hey");

    } else {
        console.log(i);
    }
}

}
count();
"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment